【问题标题】:OutOfMemoryError in BitmapFactory.decodeFile()BitmapFactory.decodeFile() 中的 OutOfMemoryError
【发布时间】:2014-12-15 09:55:41
【问题描述】:

如果图像大小大于 3 Mb android,当我从图库中选择图像时,就会出现 OutOfMemoryError。

 BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap bmp = BitmapFactory.decodeFile(imageFilePath, options);

此文本来自日志。请帮助我,因为“截止日期”)

E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.OutOfMemoryError
        at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
        at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:623)
        at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:378)
        at com.DriverNotes.AndroidMobileClientTest.ProfileActivity.onActivityResult(ProfileActivity.java:104)
        at android.app.Activity.dispatchActivityResult(Activity.java:5456)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3402)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:3449)
        at android.app.ActivityThread.access$1200(ActivityThread.java:150)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1328)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)

【问题讨论】:

  • 你必须先缩放你的图像。
  • 写下你的评论作为答案,如果这个方法有效 - 我检查你的答案是正确的

标签: android bitmap out-of-memory


【解决方案1】:

试试这个。

BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inDither=false;                     //Disable Dithering mode
bmOptions.inPurgeable=true;                   //Tell to gc that whether it needs free memory, the Bitmap can be cleared
bmOptions.inInputShareable=true;              //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
bmOptions.inTempStorage=new byte[32 * 1024];
Bitmap  mainBitmap = BitmapFactory.decodeFile(filePath, bmOptions);

【讨论】:

    【解决方案2】:

    当您的应用超出堆中分配的内存时,就会发生 OutofMemory。位图太大而无法放入内存(即堆)中。在这种情况下,您的内存不足。您需要按比例缩小位图,然后使用相同的。

    For that check this link

    试试这个代码可能对你有帮助,

     public static Bitmap decodeFile(File f,int WIDTH,int HIGHT){
     try {
         //Decode image size
         BitmapFactory.Options o = new BitmapFactory.Options();
         o.inJustDecodeBounds = true;
         BitmapFactory.decodeStream(new FileInputStream(f),null,o);
    
         //The new size we want to scale to
         final int REQUIRED_WIDTH=WIDTH;
         final int REQUIRED_HIGHT=HIGHT;
         //Find the correct scale value. It should be the power of 2.
         int scale=1;
         while(o.outWidth/scale/2>=REQUIRED_WIDTH && o.outHeight/scale/2>=REQUIRED_HIGHT)
             scale*=2;
    
         //Decode with inSampleSize
         BitmapFactory.Options o2 = new BitmapFactory.Options();
         o2.inSampleSize=scale;
         return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
     } catch (FileNotFoundException e) {}
     return null;
    }
    

    【讨论】:

    • 最终int REQUIRED_WIDTH=WIDTH;最终诠释 REQUIRED_HIGHT=HIGHT;有必要吗?
    【解决方案3】:
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 8;
    Bitmap bm = BitmapFactory.decodeFile(path,options);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-17
      • 2018-04-15
      • 1970-01-01
      相关资源
      最近更新 更多