android 类似于微博拍照上传,压缩图片,避免oom

来源:互联网 发布:nc.exe windows下载 编辑:程序博客网 时间:2024/05/18 00:05

1、压缩图片

/**

*压缩图片

**/

private Bitmap yasuo(Uri uri) {  Bitmap bitmap = null;  try {   BitmapFactory.Options options = new BitmapFactory.Options();   options.inJustDecodeBounds = true;   bitmap = BitmapFactory.decodeStream(this.getContentResolver()     .openInputStream(uri), null, options);   int picWidth = options.outWidth;   int picHeight = options.outHeight;   WindowManager windowManager = getWindowManager();   Display display = windowManager.getDefaultDisplay();   int screenWidth = display.getWidth();   int screenHeight = display.getHeight();   options.inSampleSize = 1;   if (picWidth > picHeight) {    if (picWidth > screenWidth)     options.inSampleSize = picWidth / screenWidth;   } else {    if (picHeight > screenHeight)     options.inSampleSize = picHeight / screenHeight;   }   options.inJustDecodeBounds = false;   bitmap = BitmapFactory.decodeStream(this.getContentResolver()     .openInputStream(uri), null, options);   img_touxiang.setImageBitmap(bitmap);   /*    * if (bitmap.isRecycled() == false) { bitmap.recycle(); }    */   System.gc();  } catch (Exception e1) {  }  return bitmap;}
2、压缩图片

public static Bitmap scalePicture(String filename, int maxWidth,int maxHeight) {     Bitmap bitmap = null;    try {BitmapFactory.Options opts = new BitmapFactory.Options();                        BitmapFactory.decodeFile(filename, opts);                        int srcWidth = opts.outWidth;                        int srcHeight = opts.outHeight;                        int desWidth = 0;                        int desHeight = 0;                        // 缩放比例                        double ratio = 0.0;                        if (srcWidth > srcHeight) {                                ratio = srcWidth / maxWidth;                                desWidth = maxWidth;                                desHeight = (int) (srcHeight / ratio);                        } else {                                ratio = srcHeight / maxHeight;                                desHeight = maxHeight;                                desWidth = (int) (srcWidth / ratio);                        }                        // 设置输出宽度、高度                        BitmapFactory.Options newOpts = new BitmapFactory.Options();                        newOpts.inSampleSize = (int) (ratio) + 1;                        newOpts.inJustDecodeBounds = false;                        newOpts.outWidth = desWidth;                        newOpts.outHeight = desHeight;                        bitmap = BitmapFactory.decodeFile(filename, newOpts);                } catch (Exception e) {                        // TODO: handle exception                }                return bitmap;        }
3、压缩图片

压缩图片质量:  
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.inTempStorage = new byte[16*1024];

Bitmap bitmapImage = BitmapFactory.decodeFile(path,opt); 

(几M的照片或者不行)

-----------------------------------------------------------------------------------------------------------------------

 Bitmap图片太大,会造成内存溢出。一般的都有图片预览机制,就是得到一张尺寸小一点的图片。
这里所谓的缩小尺寸可不是指在layout中设置一下宽高使图片缩小(其实质还是一张占内存大图),而是实实在在的将图片本身缩小,减小内存占用。
以下是方法,详细说明都在注释里了:
[java] 
private Bitmap revitionImageSize(String path, int size) throws IOException { 
// 取得图片 
InputStream temp = this.getAssets().open(path); 
BitmapFactory.Options options = new BitmapFactory.Options(); 
// 这个参数代表,不为bitmap分配内存空间,只记录一些该图片的信息(例如图片大小),说白了就是为了内存优化 
options.inJustDecodeBounds = true; 
// 通过创建图片的方式,取得options的内容(这里就是利用了java的地址传递来赋值) 
BitmapFactory.decodeStream(temp, null, options); 
// 关闭流 
temp.close(); 

// 生成压缩的图片 
int i = 0; 
Bitmap bitmap = null; 
while (true) { 
// 这一步是根据要设置的大小,使宽和高都能满足 
if ((options.outWidth >> i <= size) 
&& (options.outHeight >> i <= size)) { 
// 重新取得流,注意:这里一定要再次加载,不能二次使用之前的流! 
temp = this.getAssets().open(path); 
// 这个参数表示 新生成的图片为原始图片的几分之一。 
options.inSampleSize = (int) Math.pow(2.0D, i); 
// 这里之前设置为了true,所以要改为false,否则就创建不出图片 
options.inJustDecodeBounds = false; www.2cto.com

bitmap = BitmapFactory.decodeStream(temp, null, options); 
break; 

i += 1; 

return bitmap;

4、相关参考

http://www.eoeandroid.com/thread-57731-1-1.html

http://blog.csdn.net/enkezhang/article/details/8259278

http://blog.163.com/gobby_1110/blog/static/29281715201210510525413/