私は1つの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;
}
このエラーを解決する方法を教えてください。