android:外部分配对于这个过程来说太大了

时间:2012-07-26 07:50:51

标签: android memory bitmap

我的应用中出现“此进程的外部分配太大”错误。很多这些同时出现:

这是日志:

07-26 15:16:04.308: E/dalvikvm-heap(15657): 1456768-byte external allocation too large for this process.
VM won't let us allocate 1456768 bytes.
07-26 15:16:12.011: E/AndroidRuntime(15657): Uncaught handler: thread main exiting due to uncaught exception.
07-26 15:16:12.136: E/AndroidRuntime(15657): java.lang.OutOfMemoryError: bitmap size exceeds VM budget

.................

在加载大位图后,看起来它们是在渲染布局时生成的。

代码:

 private void handlePhoto(String path,int req){
 Bitmap bitmap = null;

 InputStream is = null;

BitmapFactory.Options options = new BitmapFactory.Options();

    options.inJustDecodeBounds = true; 

    options.inTempStorage =  new byte[16 * 1024];


    options.inPreferredConfig = Bitmap.Config.RGB_565;

    options.inSampleSize = 10;

    bitmap = BitmapFactory.decodeFile(path, options);

    try {
        is = new FileInputStream(path);

    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block

    Toast.makeText(SelfHelp_two_camera.this, "文件不存在",Toast.LENGTH_SHORT).show();

 e.printStackTrace();
        }

当我退出此活动时,我按如下方式使用这些:

                 if(!bm.isRecycled())

                  bm.recycle();

                  System.gc();

我也用过

private final static int CWJ_HEAP_SIZE = 8* 1024* 1024 ;

private final static float TARGET_HEAP_UTILIZATION = 0.75f;

@覆盖

protected void onCreate(Bundle savedInstanceState) {


         super.onCreate(savedInstanceState);


         VMRuntime.getRuntime().setMinimumHeapSize(CWJ_HEAP_SIZE);

         VMRuntime.getRuntime().setTargetHeapUtilization(TARGET_HEAP_UTILIZATION);

...}

我在此活动中使用了8位图。  我知道原因是内存不足,但如何解决?

0 个答案:

没有答案