gpt4 book ai didi

android - 星系 s3 中的 OutOfMemoryError

转载 作者:行者123 更新时间:2023-11-29 21:44:30 26 4
gpt4 key购买 nike

Intent localIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File dir = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
random = new Random();
seqNo = random.nextInt(1000000);
photoName = String.valueOf(seqNo);
output = new File(dir, photoName + ".jpeg");
imgPath = Uri.fromFile(output);
localIntent.putExtra(MediaStore.EXTRA_OUTPUT, imgPath);
path = output.getAbsolutePath();
startActivityForResult(localIntent, PICK_Camera_IMAGE);


protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode== PICK_Camera_IMAGE && resultCode == RESULT_OK) {


try {


File f = new File(path);

Matrix mat = new Matrix();
mat.postRotate(90);
//
Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f), null, null);
Bitmap bmpPic = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mat, true);

注意:我使用此代码使用相机拍照。它在除 samsung galaxy s3 之外的所有设备中运行。当我在 s3 上运行时,它会出现类似 OutOfMemoryError 的错误。

日志:

04-27 11:26:49.775: E/AndroidRuntime(6356): java.lang.OutOfMemoryError
04-27 11:26:49.775: E/AndroidRuntime(6356): at android.graphics.Bitmap.nativeCreate(Native Method)
04-27 11:26:49.775: E/AndroidRuntime(6356): at android.graphics.Bitmap.createBitmap(Bitmap.java:640)
04-27 11:26:49.775: E/AndroidRuntime(6356): at android.graphics.Bitmap.createBitmap(Bitmap.java:586)
04-27 11:26:49.775: E/AndroidRuntime(6356): at com.example.camera.CameraAct$10$1.run(CameraAct.java:378)
04-27 11:26:49.775: E/AndroidRuntime(6356): at android.os.Handler.handleCallback(Handler.java:615)
04-27 11:26:49.775: E/AndroidRuntime(6356): at android.os.Handler.dispatchMessage(Handler.java:92)
04-27 11:26:49.775: E/AndroidRuntime(6356): at android.os.Looper.loop(Looper.java:137)
04-27 11:26:49.775: E/AndroidRuntime(6356): at android.app.ActivityThread.main(ActivityThread.java:4898)
04-27 11:26:49.775: E/AndroidRuntime(6356): at java.lang.reflect.Method.invokeNative(Native Method)
04-27 11:26:49.775: E/AndroidRuntime(6356): at java.lang.reflect.Method.invoke(Method.java:511)
04-27 11:26:49.775: E/AndroidRuntime(6356): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)

最佳答案

您需要在使用前减少位图的样本大小。这可用于执行此操作。

private Bitmap decodeFile(File file)
{
try
{
//********************* decode image size ********************
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(file), null, options);

// ********************** Find the correct scale value. It should be the power of 2. ********************
options.inSampleSize = BitmapConverter.calculateInSampleSize(options, 145, 105);
options.inJustDecodeBounds = false;

return BitmapFactory.decodeStream(new FileInputStream(file), null, options);
}
catch(FileNotFoundException eFileNotFoundException)
{

return null;
}
}

然后你可以在需要时调用 Bitmap.recycle(),尽管我觉得没有必要。

关于android - 星系 s3 中的 OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16248855/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com