gpt4 book ai didi

android - 显示图像导致内存错误(31961104 字节分配内存不足。)

转载 作者:搜寻专家 更新时间:2023-11-01 08:52:28 24 4
gpt4 key购买 nike

我有一个应用程序可以拍照,然后在应用程序中显示它们。拍摄的第一张图片有效并显示在应用程序中,但是当拍摄第二张图片时,应用程序崩溃并且我在 logcat 的标题中收到错误。

p.s 这是 friend 写的代码,所以我不是100%确定。

代码

private PictureCallback mPicture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
mImageView = (ImageView) findViewById(R.id.mImageView);
Bitmap imageBitmap = BitmapFactory.decodeByteArray(data, 0,
data.length);

mImageView.setImageBitmap(imageBitmap);

File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (pictureFile == null) {
/*
* Log.d(TAG,
* "Error creating media file, check storage permissions: " +
* e.getMessage());
*/
return;
}
try {
SharedPreferences save = getPreferences(0);
SharedPreferences.Editor editor = save.edit();
editor.putString("oldFile", pictureFile.getAbsolutePath());

// Commit the edits!
editor.commit();
Log.v("output", "oldFile: " + oldFilePath);
File oldFile = new File(oldFilePath);
if(oldFile.delete()) // DELETING PICTURES TOO FAST.
Log.v(TAG, "Image deleted.");
oldFilePath = pictureFile.getAbsolutePath();
Log.v("output", "newFile: " + oldFilePath);

FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
}
}
};

日志

02-19 14:22:08.158: E/dalvikvm-heap(10394): Out of memory on a 31961104-byte allocation. 02-19 14:22:08.163: E/AndroidRuntime(10394): FATAL EXCEPTION: main 02-19 14:22:08.163: E/AndroidRuntime(10394): java.lang.OutOfMemoryError 02-19 14:22:08.163: E/AndroidRuntime(10394): at android.graphics.BitmapFactory.nativeDecodeByteArray(Native Method) 02-19 14:22:08.163: E/AndroidRuntime(10394): at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:551) 02-19 14:22:08.163: E/AndroidRuntime(10394): at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:569) 02-19 14:22:08.163: E/AndroidRuntime(10394): at com.example.oxplastics.MainActivity$1.onPictureTaken(MainActivity.java:331) 02-19 14:22:08.163: E/AndroidRuntime(10394): at android.hardware.Camera$EventHandler.handleMessage(Camera.java:823) 02-19 14:22:08.163: E/AndroidRuntime(10394): at android.os.Handler.dispatchMessage(Handler.java:99) 02-19 14:22:08.163: E/AndroidRuntime(10394): at android.os.Looper.loop(Looper.java:137) 02-19 14:22:08.163: E/AndroidRuntime(10394): at android.app.ActivityThread.main(ActivityThread.java:4921) 02-19 14:22:08.163: E/AndroidRuntime(10394): at java.lang.reflect.Method.invokeNative(Native Method) 02-19 14:22:08.163: E/AndroidRuntime(10394): at java.lang.reflect.Method.invoke(Method.java:511) 02-19 14:22:08.163: E/AndroidRuntime(10394): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027) 02-19 14:22:08.163: E/AndroidRuntime(10394): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) 02-19 14:22:08.163: E/AndroidRuntime(10394): at dalvik.system.NativeStart.main(Native Method)

最佳答案

在屏幕上显示图像之前,您应该对图像进行采样,查看以下链接

http://developer.android.com/training/displaying-bitmaps/index.html .

现在有可能第一次创建您的位图对象时,您的设备有足够的内存,但是当创建下一个对象时它会内存不足!!

关于android - 显示图像导致内存错误(31961104 字节分配内存不足。),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21883335/

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