拍摄照片后,我使用以下方法从相机中获取位图。在我的手机上它正常工作,但我得到了很多用户在这里得到OutOfMemory错误。我在下面包含了堆栈跟踪。
public static Bitmap getLastTakenImage(Context context) {
String[] projection = new String[]{
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DATA,
MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
MediaStore.Images.ImageColumns.DATE_TAKEN,
MediaStore.Images.ImageColumns.MIME_TYPE
};
final Cursor cursor = context.getContentResolver()
.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null,
null, MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");
if (cursor.moveToFirst()) {
String imageLocation = cursor.getString(1);
File imageFile = new File(imageLocation);
if (imageFile.exists() && imageFile.length() > 0) {
Bitmap bm = BitmapFactory.decodeFile(imageLocation);
return bm;
}
}
return null;
}
堆栈跟踪:
java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:623)
at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:378)
at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:417)
at com.app.SquareCamera.ImageUtility.getLastTakenImage(ImageUtility.java:243)
at com.app.SquareCamera.CameraFragment.onViewCreated(CameraFragment.java:141)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:973)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1181)
at android.app.Activity.performStart(Activity.java:5293)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2218)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
at android.app.ActivityThread.access$700(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5283)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
图像太大,无法在内存中充满光彩。你需要缩减它:
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 2; // Power of 2
Bitmap bm = BitmapFactory.decodeFile(imageLocation, opts);
答案 1 :(得分:1)
您应该阅读characters affected by whitespace processing
,很抱歉,我无法做出共同点。
答案 2 :(得分:0)
您需要缩放图像,以便生成Bitmap所需的存储空间更少 这是社区帖子,你可以参考: Avoid OutOfMemoryException