显示从相机拍摄的照片:android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 处的 java.lang.OutOfMemory
Posted
技术标签:
【中文标题】显示从相机拍摄的照片:android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 处的 java.lang.OutOfMemoryError【英文标题】:Showing picture taken from camera: java.lang.OutOfMemoryError at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 【发布时间】:2017-06-06 08:42:12 【问题描述】:我正在尝试显示相机拍摄的照片,有时它可以工作,但通常会给我错误:
java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:727)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:703)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:741)
at android.provider.MediaStore$Images$Media.getBitmap(MediaStore.java:847)
at com.dima.polimi.rentall.NewProduct.onActivityResult(NewProduct.java:207)
at android.app.Activity.dispatchActivityResult(Activity.java:5773)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3710)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3757)
at android.app.ActivityThread.access$1400(ActivityThread.java:170)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5635)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
我已经尝试了多个问题的解决方案,但我从未成功过
这是我的代码:
mImageView.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
dispatchTakePictureIntent();
);
dispatchTakePictureIntent
String mCurrentPhotoPath;
private void dispatchTakePictureIntent()
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getPackageManager()) != null)
// Create the File where the photo should go
File photoFile = null;
try
photoFile = createImageFile();
catch (IOException ex)
// Error occurred while creating the File
Log.i("", "IOException");
// Continue only if the File was successfully created
if (photoFile != null)
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(cameraIntent, REQUEST_TAKE_PHOTO);
private File createImageFile() throws IOException
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, // prefix
".jpg", // suffix
storageDir // directory
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK)
try
Bitmap mImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath));
mImageView.setImageBitmap(mImageBitmap);
catch (IOException e)
e.printStackTrace();
【问题讨论】:
使用image-loading library,例如Picasso,可以智能地对图像进行下采样以适应ImageView
,因此占用的内存更少。它还将在后台线程上执行该工作,而不是在主应用程序线程上执行该工作,就像您在此处所做的那样(在发生这种情况时冻结您的 UI)。
@CommonsWare 我试过了,使用:Picasso.with(this).load(mCurrentPhotoPath).into(mImageView);但它不会加载它
mCurrentPhotoPath
既不是有效的文件系统路径,也不是有效的Uri
。坚持image
,并尝试使用File
对象。
photoFile = createImageFile();
。您不应该自己创建该文件。将其留给相机应用程序。您应该只提供名称/uri。最好删除它。
此外,您应该在构建位图时调整图像大小。执行此操作的代码已在此处发布了一百次。 Google for justDecodeBounds。
【参考方案1】:
我终于让它工作了,解决方案是使用这样的位图:
Bitmap b = BitmapUtility.decodeSampledBitmapFromResource(image.getAbsolutePath(), 540, 360);
位图实用程序:
public class BitmapUtility
public static Bitmap decodeSampledBitmapFromResource(String path, int reqWidth, int reqHeight)
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path,options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
private static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight)
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth)
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth)
inSampleSize *= 2;
return inSampleSize;
【讨论】:
以上是关于显示从相机拍摄的照片:android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 处的 java.lang.OutOfMemory的主要内容,如果未能解决你的问题,请参考以下文章