Android 仅Galaxy Note 3内存不足错误
我正在开发一个应用程序,可以显示一张照片从相机使用相机意图使用额外的作物选项。该代码在大多数设备上运行良好,但当我尝试在我的全新Galaxy Note3中测试它时,它崩溃了,并且没有正常运行,而且拍摄的图像仍然很大,“几乎4 MB”,大到可以显示在imageview中。有人能告诉我是否有什么办法可以避免这种情况吗 我的代码如下:Android 仅Galaxy Note 3内存不足错误,android,android-intent,camera,imageview,Android,Android Intent,Camera,Imageview,我正在开发一个应用程序,可以显示一张照片从相机使用相机意图使用额外的作物选项。该代码在大多数设备上运行良好,但当我尝试在我的全新Galaxy Note3中测试它时,它崩溃了,并且没有正常运行,而且拍摄的图像仍然很大,“几乎4 MB”,大到可以显示在imageview中。有人能告诉我是否有什么办法可以避免这种情况吗 我的代码如下: Intent intent = new Intent( "android.media.a
Intent intent = new Intent(
"android.media.action.IMAGE_CAPTURE");
file = getOutputMediaFile();
intent.putExtra("crop", "true");
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(file));
intent.putExtra("outputFormat",
Bitmap.CompressFormat.JPEG
.toString());
intent.putExtra(
MediaStore.EXTRA_SCREEN_ORIENTATION,
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
startActivityForResult(intent,
ACTION_REQUEST_CAMERA);
以及“活动”和“结果”
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case ACTION_REQUEST_CAMERA:
if (data != null) {
try {
int inWidth = 0;
int inHeight = 0;
InputStream in = new FileInputStream(
file.getAbsolutePath());
// decode image size (decode metadata only, not the
// whole image)
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, options);
in.close();
in = null;
// save width and height
inWidth = options.outWidth;
inHeight = options.outHeight;
// decode full image pre-resized
in = new FileInputStream(file.getAbsolutePath());
options = new BitmapFactory.Options();
// calc rought re-size (this is no exact resize)
options.inSampleSize = Math.max(inWidth / 350,
inHeight / 550);
// decode full image
Bitmap roughBitmap = BitmapFactory.decodeStream(in,
null, options);
// calc exact destination size
Matrix m = new Matrix();
RectF inRect = new RectF(0, 0, roughBitmap.getWidth(),
roughBitmap.getHeight());
RectF outRect = new RectF(0, 0, 700, 800);
m.setRectToRect(inRect, outRect,
Matrix.ScaleToFit.CENTER);
float[] values = new float[9];
m.getValues(values);
// resize bitmap
Bitmap resizedBitmap = Bitmap.createScaledBitmap(
roughBitmap,
(int) (roughBitmap.getWidth() * values[0]),
(int) (roughBitmap.getHeight() * values[4]),
true);
// save image
try {
FileOutputStream out = new FileOutputStream(
file.getAbsolutePath());
resizedBitmap.compress(Bitmap.CompressFormat.JPEG,
90, out);
fullphoto = resizedBitmap;
setPic(file.getAbsolutePath(), camera);
} catch (Exception e) {
Log.e("Image", e.getMessage(), e);
}
} catch (IOException e) {
Log.e("Image", e.getMessage(), e);
}
}
// fullphoto = BitmapFactory.decodeFile(file.getAbsolutePath());
// photo = decodeSampledBitmapFromFile(file.getAbsolutePath(),
// 100, 100);
// camera.setImageBitmap(imghelper.getRoundedCornerBitmap(
// fullphoto, 10));
iscamera = "Yes";
firsttime = false;
break;
}
在创建
resizedBitmap
之后和将resizedBitmap
写入文件之前,您是否尝试调用roughBitmap.recycle()
?我会尝试给您反馈…谢谢!