Android Bitmap Factory内存不足第二张照片

我有处理相机的片段.我的问题是它需要一张照片并将其显示在imageView上,就好了.

我第二次尝试拍摄一张OutOfMemory错误.任何提示如何在第一张照片后释放内存?

 @Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);   
        System.out.println("tesstssaffsafdfsdfsd");
        switch (requestCode) {
        case TAKE_PICTURE:
            if (resultCode == Activity.RESULT_OK) {
                getActivity().getContentResolver().notifyChange(mUri, null);
                ContentResolver cr = getActivity().getContentResolver();
                try {
                    mPhoto = android.provider.MediaStore.Images.Media.getBitmap(cr, mUri);
                 ((ImageView)rootView.findViewById(R.id.snap)).setImageBitmap(mPhoto);
                } catch (Exception e) {
                     Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
                   }
              }
        }




 }

这是堆栈跟踪:

08-21 16:33:27.450: E/AndroidRuntime(1840): FATAL EXCEPTION: main
08-21 16:33:27.450: E/AndroidRuntime(1840): java.lang.OutOfMemoryError
08-21 16:33:27.450: E/AndroidRuntime(1840):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
08-21 16:33:27.450: E/AndroidRuntime(1840):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:650)
08-21 16:33:27.450: E/AndroidRuntime(1840):     at   android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:722)
08-21 16:33:27.450: E/AndroidRuntime(1840):     at android.provider.MediaStore$Images$Media.getBitmap(MediaStore.java:790)
08-21 16:33:27.450: E/AndroidRuntime(1840):     at com.example.lemonrelative.reading.AddReadingFrag_3.onActivityResult(AddReadingFrag_3.java:113)
08-21 16:33:27.450: E/AndroidRuntime(1840):     at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:166)
08-21 16:33:27.450: E/AndroidRuntime(1840):     at android.app.Activity.dispatchActivityResult(Activity.java:5390)
08-21 16:33:27.450: E/AndroidRuntime(1840):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3178)
08-21 16:33:27.450: E/AndroidRuntime(1840):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3225)
08-21 16:33:27.450: E/AndroidRuntime(1840):     at android.app.ActivityThread.access$1100(ActivityThread.java:140)
08-21 16:33:27.450: E/AndroidRuntime(1840):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1275)
08-21 16:33:27.450: E/AndroidRuntime(1840):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-21 16:33:27.450: E/AndroidRuntime(1840):     at android.os.Looper.loop(Looper.java:137)
08-21 16:33:27.450: E/AndroidRuntime(1840):     at android.app.ActivityThread.main(ActivityThread.java:4898)
08-21 16:33:27.450: E/AndroidRuntime(1840):     at java.lang.reflect.Method.invokeNative(Native Method)
08-21 16:33:27.450: E/AndroidRuntime(1840):     at  java.lang.reflect.Method.invoke(Method.java:511)
08-21 16:33:27.450: E/AndroidRuntime(1840):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
08-21 16:33:27.450: E/AndroidRuntime(1840):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
08-21 16:33:27.450: E/AndroidRuntime(1840):     at dalvik.system.NativeStart.main(Native Method)

解决方法:

相机拍摄的全尺寸照片相当大.假设可能有400万像素,如果你使用RGB8888作为位图读取,它将花费大约16mb.如果要同时加载2张图片,可能会内存不足.

有几种方法可以帮助.

>使用样本大小读取位图,这将降低位图的分辨率.有关详情,请参见this page.
>使用RGB565代替RGB8888读取位图.由于您的位图将是不透明的,因此Alpha通道将毫无用处.
>在创建新位图之前回收旧位图.这是你可以尝试的拳头.

请参阅下面的示例代码.

ImageView mImage = xxx;
BitmapDrawable drawable = (BitmapDrawable) mImage.getDrawable(); 
if(drawable != null && drawable.getBitmap() != null){
    drawable.getBitmap().recycle();
    mImage.setImageDrawable(null); 
}
// set new bitmap here.
上一篇:Android笔记(二十四) Android中的SeekBar(拖动条)


下一篇:我可以在桌面应用程序中使用Android SDK类吗?