0

内部メモリに保存されている 199KB の画像があります。私のデバイスの RAM の合計は 512 MB です。Android のバージョンは 2.3.5 です。だから私がやっていることは、すべての画像を実行時にビットマップ描画可能にすることです。

次の例外が発生します。

10-10 14:16:03.782: E/dalvikvm-heap(20331): 73632-byte external allocation too large for this process.
10-10 14:16:03.782: E/dalvikvm(20331): Out of memory: Heap Size=6599KB, Allocated=3949KB, Bitmap Size=13871KB, Limit=20480KB
10-10 14:16:03.782: E/dalvikvm(20331): Trim info: Footprint=6599KB, Allowed Footprint=6599KB, Trimmed=264KB
10-10 14:16:03.792: E/GraphicsJNI(20331): VM won't let us allocate 73632 bytes

これが私のコードです:

File myDir =act.getApplicationContext().getDir(CURRENT_THEME, act.MODE_PRIVATE);
public BitmapDrawable getResourceFromInternalStorage(String resourceName)
 {
  // Runtime.getRuntime().gc();
    String filename = resourceName;
         File file = new File(myDir, filename+".png");
   Resources res = act.getResources();
   BitmapDrawable bd =null;
   bitmap=null;
   try
   {
//    BitmapFactory.Options options = new BitmapFactory.Options();
//    options.inJustDecodeBounds = true;
//    options.inSampleSize = 32;


              BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
                   bmpFactoryOptions.inJustDecodeBounds=true;
                   BitmapFactory.decodeFile(file.getPath(),bmpFactoryOptions);

                   long reqsize=bmpFactoryOptions.outWidth*bmpFactoryOptions.outHeight*2;
                   long allocNativeHeap = Debug.getNativeHeapAllocatedSize();


                   final long heapPad=(long) Math.max(4*1024*1024,Runtime.getRuntime().maxMemory()*0.1);
                   if ((reqsize + allocNativeHeap + heapPad) >= Runtime.getRuntime().maxMemory())
                   {
                    Toast.makeText(act, "exception", Toast.LENGTH_LONG).show();
//                      bitmap.recycle();
                  //  Runtime.getRuntime().gc();
//                    System.gc();
                    //  return null;

                   }
                   bmpFactoryOptions.inJustDecodeBounds=false;
                   bitmap=BitmapFactory.decodeFile(file.getPath(),bmpFactoryOptions);
          bd = new BitmapDrawable(res, bitmap);
   }
   catch(java.lang.OutOfMemoryError e)
   {
    Log.v("Exceptional exception", e.getMessage());
    bitmap.recycle();
    System.gc();
   }
   finally{

   }

         return bd;
 }
4

2 に答える 2

1

ビットマップでリサイクルしてください。あなたはそれをやっていますが、キャッチブロックです。

try ブロック内:

bitmap.recycle();
bitmap=null;

上記のコードを試して確認してください。

于 2013-10-10T09:25:14.797 に答える