Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.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
Java 安卓系统sd卡存储高质量位图时如何避免OutOfMemory错误_Java_Android_Stream_Android Bitmap - Fatal编程技术网

Java 安卓系统sd卡存储高质量位图时如何避免OutOfMemory错误

Java 安卓系统sd卡存储高质量位图时如何避免OutOfMemory错误,java,android,stream,android-bitmap,Java,Android,Stream,Android Bitmap,我想将位图存储到SD卡中,但当我选择大位图时,我遇到了“内存不足错误”的问题 我的代码: FileOutputStream out = new FileOutputStream(file); bm.compress(Bitmap.CompressFormat.JPEG, 100, out); out.flush(); out.close(); 我读了很多可能的问题,但它们的答案并不能解决我的问题,因为所有的答案都表明必须以低质量保存位图 请不要回答我降低质量,因为我需要以高质

我想将位图存储到SD卡中,但当我选择大位图时,我遇到了“内存不足错误”的问题

我的代码:

FileOutputStream out = new FileOutputStream(file);         
bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
我读了很多可能的问题,但它们的答案并不能解决我的问题,因为所有的答案都表明必须以低质量保存位图


请不要回答我降低质量,因为我需要以高质量存储位图。

在加载位图之前,您可以通过

BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
btmapOptions.inJustDecodeBounds = true;
如果内存超过,则必须调整位图的大小,最终设置

BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
    btmapOptions.inJustDecodeBounds = false; 
加载位图。 要增加分配的内存,可以使用
largeHeap
设置为
true
,但这只会增加分配的内存

调整位图大小的示例代码

public 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;
    }

坦克,我如何将其与我的代码关联?如何使用?您必须根据可用内存加载位图。btmapOptions.inJustDecodeBounds=true;不会加载内存,因此,您可以检查并调整大小以找到调整后的大小,并根据调整后的大小加载图像。要使用此选项,我需要实例选项可能
BitmapFactory.decodeFile(文件路径,选项)但我的位图没有任何路径。我尝试将位图转换为使用
BitmapFactory.decodeFile(byteArray,选项)的byteArray
但它也面临outOfMemory问题。我知道我必须使用它,但我不知道如何使用?FileOutputStream out=新FileOutputStream(file);显示您正在写入文件,我错了吗<将压缩位图存储到目录中需要代码>文件输出流输出
。bm是我要存储在SD卡中的输入。bm没有任何目录,因为我是通过canvas创建的。