【问题标题】:Android out of memory Bound ExceptionAndroid内存不足绑定异常
【发布时间】:2013-10-10 09:15:25
【问题描述】:

我有 199KB 的图像存储在内部存储器中。我的设备的总内存为 512 MB。安卓版本为 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;
 }

【问题讨论】:

  • 几天前我遇到了同样的问题,但我通过为模拟器分配更多内存来解决它。
  • @BlueGreen 我无法为设备上的 ram 分配更多大小,并且我的目标不是模拟器它的设备

标签: android bitmap bitmapfactory


【解决方案1】:

在您的位图上进行回收。您正在执行此操作,但处于 catch 块中。

在尝试块中:

bitmap.recycle();
bitmap=null;

尝试上面的代码并检查。

【讨论】:

  • 我已经尝试过了,但在尝试块位图为空。所以它会在回收时抛出异常。
  • 做一件事,比如 if(bitmap!=null{bitmap.recycle();bitmap=null;}
  • 我仍然遇到异常
【解决方案2】:
猜你喜欢
  • 2012-10-24
  • 2023-03-06
  • 1970-01-01
  • 2017-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-10
  • 2010-10-05
相关资源
最近更新 更多