我有处理相机的片段。我的问题是它需要一张照片并将其显示在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)
答案 0 :(得分:6)
如果第一个位图的使用完成,请重新编写或 最简单的方法是将这两行添加到清单到应用程序标记
中android:hardwareAccelerated="false"
android:largeHeap="true"
但是,如果您正在构建一个内存效率高的应用程序,则不推荐这些。但这确实有效。
答案 1 :(得分:2)
相机拍摄的全尺寸照片非常大。假设可能有400万像素,如果你使用RGB8888作为位图读取,它将花费大约16mb。如果您想同时加载2张图片,可能会内存不足。
有几种方法可以提供帮助。
请参阅下面的示例代码。
ImageView mImage = xxx;
BitmapDrawable drawable = (BitmapDrawable) mImage.getDrawable();
if(drawable != null && drawable.getBitmap() != null){
drawable.getBitmap().recycle();
mImage.setImageDrawable(null);
}
// set new bitmap here.