android 類似於微博拍照上傳,壓縮圖片,避免oom

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/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章