2012-11-30 32 views
0

Possible Duplicate:
bitmap size exceeds Vm budget error android位圖大小超過Android的

VM預算我已經開發了一個機器人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; 
    } 

請幫我解決這個錯誤。

+0

檢查圖像分辨率和CONVER成低分辨率,您可以使用PNG文件類型...並 – Hemant

+0

退房此developer.android鏈接: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html –

回答

1

您只需要更仔細地進行搜索。

這是最好的IMO,link這可能有助於回答你的問題。

+0

謝謝你的答案。這對我真的很有幫助 – user1859172

相關問題