0

可能重复:
位图大小超过 Vm 预算错误 android

我开发了一个 android example.here 我得到了以下错误:

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;
    }

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

4

3 回答 3

2

您可以使用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

于 2012-11-30T05:04:50.943 回答
1

您只需要更加谨慎地进行搜索。

这是最好的 IMO链接,可能有助于回答您的问题。

于 2012-11-30T05:03:33.610 回答
0

-这是位图的常见问题,它是在加载位图导致内存超出范围时引起的。

解决方法见这个官方链接:

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

于 2012-11-30T05:06:27.637 回答