java - 位图大小超出 Android 中的 VM 预算

标签 java android bitmap bitmapimage bitmapfactory

Possible Duplicate:
bitmap size exceeds Vm budget error android

我开发了一个android示例。这里我得到了以下错误:

java.lang.OutOfMemoryError: bitmap size exceeds VM budget

这里我的控制台窗口显示以下错误:

11-23 22:15:55.933: E/dalvikvm-heap(2522): 22458-byte external allocation too large for this process.
11-23 22:15:55.933: E/GraphicsJNI(2522): VM won't let us allocate 22458 bytes
11-23 22:15:55.993: D/AndroidRuntime(2522): Shutting down VM
11-23 22:15:55.993: W/dalvikvm(2522): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-23 22:15:56.054: E/AndroidRuntime(2522): FATAL EXCEPTION: main
11-23 22:15:56.054: E/AndroidRuntime(2522): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ssmobileproductions.catalogue/com.ssmobileproductions.catalogue.SingleMenuItem}: android.view.InflateException: Binary XML file line #93: Error inflating class android.widget.ImageButton
11-23 22:15:56.054: E/AndroidRuntime(2522):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
 11-23 22:15:56.054: E/AndroidRuntime(2522):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
 11-23 22:15:56.054: E/AndroidRuntime(2522):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-23 22:15:56.054: E/AndroidRuntime(2522):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-23 22:15:56.054: E/AndroidRuntime(2522):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-23 22:15:56.054: E/AndroidRuntime(2522):     at android.os.Looper.loop(Looper.java:123)
 11-23 22:15:56.054: E/AndroidRuntime(2522):    at android.app.ActivityThread.main(ActivityThread.java:4627)
 11-23 22:15:56.054: E/AndroidRuntime(2522):    at java.lang.reflect.Method.invokeNative(Native Method)
 11-23 22:15:56.054: E/AndroidRuntime(2522):    at java.lang.reflect.Method.invoke(Method.java:521)
 11-23 22:15:56.054: E/AndroidRuntime(2522):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
 11-23 22:15:56.054: E/AndroidRuntime(2522):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
 11-23 22:15:56.054: E/AndroidRuntime(2522):    at dalvik.system.NativeStart.main(Native Method)
 11-23 22:15:56.054: E/AndroidRuntime(2522): Caused by: android.view.InflateException: Binary XML file line #93: Error inflating class android.widget.ImageButton
 11-23 22:15:56.054: E/AndroidRuntime(2522):    at android.view.LayoutInflater.createView(LayoutInflater.java:513)
 11-23 22:15:56.054: E/AndroidRuntime(2522):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
 11-23 22:15:56.054: E/AndroidRuntime(2522):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
 11-23 22:15:56.054: E/AndroidRuntime(2522):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
 11-23 22:15:56.054: E/AndroidRuntime(2522):    at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
 11-23 22:15:56.054: E/AndroidRuntime(2522):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
 11-23 22:15:56.054: E/AndroidRuntime(2522):    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
 11-23 22:15:56.054: E/AndroidRuntime(2522):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
 11-23 22:15:56.054: E/AndroidRuntime(2522):    at android.app.Activity.setContentView(Activity.java:1647)
 11-23 22:15:56.054: E/AndroidRuntime(2522):    at com.ssmobileproductions.catalogue.SingleMenuItem.onCreate(SingleMenuItem.java:35)
 11-23 22:15:56.054: E/AndroidRuntime(2522):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
 11-23 22:15:56.054: E/AndroidRuntime(2522):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
 11-23 22:15:56.054: E/AndroidRuntime(2522):    ... 11 more
 11-23 22:15:56.054: E/AndroidRuntime(2522): Caused by: java.lang.reflect.InvocationTargetException
 11-23 22:15:56.054: E/AndroidRuntime(2522):    at android.widget.ImageButton.<init>(ImageButton.java:78)
 11-23 22:15:56.054: E/AndroidRuntime(2522):    at java.lang.reflect.Constructor.constructNative(Native Method)
 11-23 22:15:56.054: E/AndroidRuntime(2522):    at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
 11-23 22:15:56.054: E/AndroidRuntime(2522):    at android.view.LayoutInflater.createView(LayoutInflater.java:500)
 11-23 22:15:56.054: E/AndroidRuntime(2522):    ... 22 more
 11-23 22:15:56.054: E/AndroidRuntime(2522): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
 11-23 22:15:56.054: E/AndroidRuntime(2522):    at android.graphics.Bitmap.nativeCreate(Native Method)
 11-23 22:15:56.054: E/AndroidRuntime(2522):    at android.graphics.Bitmap.createBitmap(Bitmap.java:468)
  11-23 22:15:56.054: E/AndroidRuntime(2522):   at android.graphics.Bitmap.createBitmap(Bitmap.java:435)
 11-23 22:15:56.054: E/AndroidRuntime(2522):    at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:340)
 11-23 22:15:56.054: E/AndroidRuntime(2522):    at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:488)
 11-23 22:15:56.054: E/AndroidRuntime(2522):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:462)
 11-23 22:15:56.054: E/AndroidRuntime(2522):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:323)
  11-23 22:15:56.054: E/AndroidRuntime(2522):   at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
  11-23 22:15:56.054: E/AndroidRuntime(2522):   at android.content.res.Resources.loadDrawable(Resources.java:1709)
   11-23 22:15:56.054: E/AndroidRuntime(2522):  at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
  11-23 22:15:56.054: E/AndroidRuntime(2522):   at android.widget.ImageView.<init>(ImageView.java:118)
  11-23 22:15:56.054: E/AndroidRuntime(2522):   at android.widget.ImageButton.<init>(ImageButton.java:82)
  11-23 22:15:56.054: E/AndroidRuntime(2522):   ... 26 more

这是我的位图代码:

 private Bitmap getBitmap(String url) 
{
    File f=fileCache.getFile(url);

    //from SD cache
    Bitmap b = decodeFile(f);
    if(b!=null)
        return b;

    //from web
    try {
        Bitmap bitmap=null;
        URL imageUrl = new URL(url);
        HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
        conn.setConnectTimeout(30000);
        conn.setReadTimeout(30000);
        conn.setInstanceFollowRedirects(true);
        InputStream is=conn.getInputStream();
        OutputStream os = new FileOutputStream(f);
        Utils.CopyStream(is, os);
        os.close();
        bitmap = decodeFile(f);
        return bitmap;
    } catch (Exception ex){
       ex.printStackTrace();
       return null;
    }
}

//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f){
    try {
        //decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f),null,o);

        //Find the correct scale value. It should be the power of 2.
        final int REQUIRED_SIZE=70;
        int width_tmp=o.outWidth, height_tmp=o.outHeight;
        int scale=1;
        while(true){
            if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                break;
            width_tmp/=2;
            height_tmp/=2;
            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;
    }

请帮助我如何解决此错误。

最佳答案

您可以使用opts.inSampleSize=2; or opts.inSampleSize=4解决该问题(question)

BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize=2;
Bitmap bitmap = decodeFile(f,opts);`

本文还提供了一些可能有帮助的提示:

http://android-developers.blogspot.de/2009/01/avoiding-memory-leaks.html

关于java - 位图大小超出 Android 中的 VM 预算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13639277/

相关文章:

java - 来自 Android 的 JSON 请求未执行

java - 尝试使用 Java KeyStore 类将自签名 CA 证书导入 Windows 根信任库时无法阻止/绕过用户提示

java - 工具栏不会显示在 fragment 上

Android 圆位图中心标签不正确居中

java - JUNG 图可视化中的顶点标签

java - 单选按钮 : Listener in XML or JAVA?

android-streaming mp3 文件到其他设备

android - 将 View 放在滑动抽屉 Handlebars 上方

c# - 如何返回位图类型?

android - 如何从图片中获取元数据?