android图片压缩
来源:互联网 发布:藏头诗在线生成器软件 编辑:程序博客网 时间:2024/05/01 03:13
andriod提供了一些方法如下:
压缩图片质量:
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, fos);
其中的quality为0~100, 可以压缩图片质量, 不过对于大图必须对图片resize
这个是等比例缩放:
bitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);
这个是截取图片某部分:
bitmap = Bitmap.createBitmap(bitmap, x, y, width, height);
压缩图片质量:
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, fos);
其中的quality为0~100, 可以压缩图片质量, 不过对于大图必须对图片resize
这个是等比例缩放:
bitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);
这个是截取图片某部分:
bitmap = Bitmap.createBitmap(bitmap, x, y, width, height);
这几个方法都是针对Bitmap的, 不过鉴于Bitmap可以从file中读取, 也可以写入file
这段代码很多网站都有转载,在这里是为了注释:较大的图片文件上传到服务器一般都需要压缩调整,保证数据通信的效率是最主要的。
- //对图片进行压缩
- BitmapFactory.Options options = new BitmapFactory.Options();
- options.inJustDecodeBounds = true;
- //获取这个图片的宽和高
- Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/dcim/Camera/hello.jpg",options);//此时返回bm为空
- options.inJustDecodeBounds =false;
- //计算缩放比
- int be = (int)(options.outHeight / (float)200);
- if(be <= 0)
- be =1;
- options.inSampleSize =be;
- //重新读入图片,注意这次要把options.inJustDecodeBounds设为false哦
- bitmap = BitmapFactory.decodeFile("/sdcard/dcim/Camera/hello.jpg",options);
- int w = bitmap.getWidth();
- int h=bitmap.getHeight();
- System.out.println(w+" "+h);
- myImageView.setImageBitmap(bitmap);
- //保存入sdCard
- File file2= new File("/sdcard/dcim/Camera/test.jpg");
- try {
- FileOutputStream out = new FileOutputStream(file2);
- if(bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)){
- out.flush();
- out.close();
- }
- } catch (Exception e) {
- // TODO: handle exception
- }
- //读取sd卡
- File file =new File("/sdcard/dcim/Camera/test.jpg");
- int maxBufferSize = 16 * 1024;
- int len = 0;
- ByteArrayOutputStream outStream = new ByteArrayOutputStream();
- BufferedInputStream bufferedInputStream;
- try {
- bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
- int bytesAvailable = bufferedInputStream.available();
- int bufferSize = Math.min(bytesAvailable, maxBufferSize);
- byte[] buffer = new byte[bufferSize];
- while ((len = bufferedInputStream.read(buffer)) != -1)
- {
- outStream.write(buffer, 0, bufferSize);
- }
- data = outStream.toByteArray();
- outStream.close();
- bufferedInputStream.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- android图片压缩方法
- android图片压缩方法
- android图片压缩
- android图片压缩
- Android图片压缩
- android图片压缩
- Android BitmapFactory 图片压缩
- android图片压缩
- android图片压缩总结
- android图片压缩一:
- Android 图片压缩二:
- Android图片压缩总结
- Android开发压缩图片
- Android图片压缩
- android 的图片压缩
- Android关于图片压缩
- android 图片压缩
- android图片压缩上传
- 使用 /sys 文件系统访问 Linux 内核
- 用AIR选项卡式工程嵌入AWAY3D时,注意选项卡的backgroundAlpha属性
- extends Thread 与 implements Runnable 的区别
- MFC C++ 由于应用程序配置不正确,应用程序未能启动。重新安装应用程序可能会纠正这个问题
- SUSE LINUX系统文件句柄限制的修改
- android图片压缩
- excel公式概述
- android android:taskAffinity 详解
- 重新认识oo'World
- ARP, Address Resolution Protocol
- Samsung手机验证
- ActivityGroup切换Activity时非全屏
- FxCop in CruiseControl.NET
- 驱动程序中几个常见例程(读书笔记)