Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android BitmapFactory解码资源内存不足异常_Android_Android Image_Android Bitmap_Android File_Bitmapfactory - Fatal编程技术网

Android BitmapFactory解码资源内存不足异常

Android BitmapFactory解码资源内存不足异常,android,android-image,android-bitmap,android-file,bitmapfactory,Android,Android Image,Android Bitmap,Android File,Bitmapfactory,我是android新手,正在开发一款应用程序,可以将大型图像从可绘图文件夹保存到手机存储中。这些文件的分辨率为2560x2560,我希望保存这些文件时不会丢失图像质量 我使用下面的方法保存图像,它会给我内存不足异常。我已经看到了许多关于如何高效加载大型位图的答案。但是我真的找不到这个问题的答案 在我的代码中,我使用 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), imageId); File file = new File

我是android新手,正在开发一款应用程序,可以将大型图像从可绘图文件夹保存到手机存储中。这些文件的分辨率为2560x2560,我希望保存这些文件时不会丢失图像质量

我使用下面的方法保存图像,它会给我内存不足异常。我已经看到了许多关于如何高效加载大型位图的答案。但是我真的找不到这个问题的答案

在我的代码中,我使用

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), imageId);
File file = new File(root.getAbsolutePath() + "/Pictures/" + getResources().getString(R.string.app_name) + "/" + timeStamp + ".jpg");
file.createNewFile();
FileOutputStream oStream = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 100, oStream);
oStream.close();
bitmap.recycle();
我的代码有什么问题吗?对于较小的图像,这一点毫无例外

如果我使用android:largeHeap=“true”,这不会引发任何异常。但是我知道使用android:largeHeap=“true”不是一个好的做法

是否有任何有效的方法可以毫无例外地从drawable文件夹保存大型图像


提前谢谢。

如果您只想复制图像文件,首先不应该将其解码为位图

您可以使用以下方法复制原始资源文件,例如:

InputStream in = getResources().openRawResource(imageId);
String path = root.getAbsolutePath() + "/Pictures/" + getResources().getString(R.string.app_name) + "/" + timeStamp + ".jpg";
FileOutputStream out = new FileOutputStream(path);
try {
    byte[] b = new byte[4096];
    int len = 0;
    while ((len = in.read(b)) > 0) {
        out.write(b, 0, len);
    }
}
finally {
    in.close();
    out.close();
}

请注意,您必须将图像存储在
res/raw/
目录中,而不是
res/drawable/

如果您只想复制图像文件,首先不应将其解码为位图

您可以使用以下方法复制原始资源文件,例如:

InputStream in = getResources().openRawResource(imageId);
String path = root.getAbsolutePath() + "/Pictures/" + getResources().getString(R.string.app_name) + "/" + timeStamp + ".jpg";
FileOutputStream out = new FileOutputStream(path);
try {
    byte[] b = new byte[4096];
    int len = 0;
    while ((len = in.read(b)) > 0) {
        out.write(b, 0, len);
    }
}
finally {
    in.close();
    out.close();
}

请注意,您必须将图像存储在
res/raw/
目录中,而不是
res/drawable/

中。或者,将它们存储为资产(例如,在Android Studio模块的
src/main/assets/
中),然后从那里复制它们(在通过调用
getAssets()
获得的
AssetManager
上调用
open()
)。但我将相同的图像加载到我的应用程序的ImageView(使用davemorrissey称为子采样比例图像视图的外部库)。因此,如果我将这些图像放在raw文件夹中,应用程序就会再次崩溃,并且出现内存不足的异常(在android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:500))。@VajiraLasantha它会崩溃,因为您的设备没有足够的RAM来保存图像。您的
ImageView
应该比屏幕小,因此您可以调整图像大小以适合
ImageView
并显示它。这里有一个有用的链接:@Floern修复了它。当从原始图像文件解码位图以显示在ImageView中时,使用Bitmap.Config.RGB_565和inDither=true选项,现在可以正常工作。谢谢你的回答。我同意。或者,将它们存储为资产(例如,在Android Studio模块的
src/main/assets/
中),然后从那里复制它们(在通过调用
getAssets()
获得的
AssetManager
上调用
open()
)。但我将相同的图像加载到我的应用程序的ImageView(使用davemorrissey称为子采样比例图像视图的外部库)。因此,如果我将这些图像放在raw文件夹中,应用程序就会再次崩溃,并且出现内存不足的异常(在android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:500))。@VajiraLasantha它会崩溃,因为您的设备没有足够的RAM来保存图像。您的
ImageView
应该比屏幕小,因此您可以调整图像大小以适合
ImageView
并显示它。这里有一个有用的链接:@Floern修复了它。当从原始图像文件解码位图以显示在ImageView中时,使用Bitmap.Config.RGB_565和inDither=true选项,现在可以正常工作。谢谢你的回答。