OutofMemoryError:位图大小超过VM预算(Android)

Chr*_*pix 22 android exception bitmap dalvik

在BitmapFactory中获取异常.不确定是什么问题.(我可以猜到这个问题,但不确定为什么会发生这种情况)

ERROR/AndroidRuntime(7906): java.lang.OutOfMemoryError: bitmap size exceeds VM budget

ERROR/AndroidRuntime(7906):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:295)

我的代码很简单.我定义了一个带有默认图像的XML布局.我尝试在SD卡上加载一个bm(​​如果存在 - 它是).如果不是,则显示默认图像.无论如何..这是代码:

public class showpicture extends Activity {
  public void onCreate(Bundle savedInstanceState) {

         /** Remove menu/status bar **/
         requestWindowFeature(Window.FEATURE_NO_TITLE);
         final Window win = getWindow();   
         win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

            Bitmap bm;
         super.onCreate(savedInstanceState);
         setContentView(R.layout.showpicture);
            try {
         ImageView mImageButton = (ImageView)findViewById(R.id.displayPicture);
         bm = Bitmap.createScaledBitmap(BitmapFactory.decodeFile("/sdcard/dcim/Camera/20091018203339743.jpg"),100, 100, true);
         parkImageButton.setImageBitmap(bm);
         }
         catch (IllegalArgumentException ex) {
          Log.d("MYAPP",ex.getMessage());
         } 
            catch (IllegalStateException ex) {
Run Code Online (Sandbox Code Playgroud)

它没有bm=Bitmap.createScaledBitmap任何想法?我在论坛上做了一些研究,它指出了这篇文章, 我只是不知道为什么它不起作用.任何帮助都会很棒!谢谢,

克里斯.

M. *_*enk 13

inSampleSize是一个很好的提示.但是固定值通常不能很好地工作,因为来自文件的大位图通常是用户文件,这些用户文件可以从微小的缩略图变为数码相机的12MP图像.

这是一个快速而肮脏的加载程序.我知道还有改进的余地,比如一个更好的编码循环,使用2的幂来加快解码速度,等等.但这是一个有效的开始......

public static Bitmap loadResizedBitmap( String filename, int width, int height, boolean exact ) {
    Bitmap bitmap = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile( filename, options );
    if ( options.outHeight > 0 && options.outWidth > 0 ) {
        options.inJustDecodeBounds = false;
        options.inSampleSize = 2;
        while (    options.outWidth  / options.inSampleSize > width
                && options.outHeight / options.inSampleSize > height ) {
            options.inSampleSize++;
        }
        options.inSampleSize--;

        bitmap = BitmapFactory.decodeFile( filename, options );
        if ( bitmap != null && exact ) {
            bitmap = Bitmap.createScaledBitmap( bitmap, width, height, false );
        }
    }
    return bitmap;
}
Run Code Online (Sandbox Code Playgroud)

顺便说一句,在较新的API中,还有很多BitmapFactory.Option用于将图像拟合到屏幕DPI,但我不确定它们是否真的简化了任何事情.使用android.util.DisplayMetrics.density或简单的固定大小以减少内存消耗似乎更好地工作.


小智 5

请参考此链接,请注意outOfMemory错误可通过以下方式解决:

public Bitmap decodeFile(String filePath) {

Bitmap bitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inPurgeable = true;

try {
BitmapFactory.Options.class.getField("inNativeAlloc").setBoolean(options,true);

} catch (IllegalArgumentException e) {
    e.printStackTrace();
} catch (SecurityException e) {
    e.printStackTrace();
} catch (IllegalAccessException e) {
    e.printStackTrace();
} catch (NoSuchFieldException e) {
    e.printStackTrace();
}

if(filePath != null)
{
    bitmap = BitmapFactory.decodeFile(filePath, options);               
}

return bitmap;
}
Run Code Online (Sandbox Code Playgroud)


Ste*_*hih 4

http://code.google.com/p/android/issues/detail?id=8488

http://mobi-solutions.blogspot.com/2010/08/how-to-if-you-want-to-create-and.html

  • 我讨厌只是链接的答案。不要误会我的意思,链接很好,但是仅仅显示一个没有解释的链接确实让人感觉很懒。 (31认同)