Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/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 bitmap.compress from Uri导致OutOfMemoryError_Android_Bitmap_Out Of Memory - Fatal编程技术网

Android bitmap.compress from Uri导致OutOfMemoryError

Android bitmap.compress from Uri导致OutOfMemoryError,android,bitmap,out-of-memory,Android,Bitmap,Out Of Memory,我正在尝试将用户选择的位图保存到我自己的应用程序路径中 不幸的是,对于非常大的图像,我得到了记忆错误 我正在使用以下代码: private String loadImage (Uri filePath) { File fOut = new File(getFilesDir(),"own.jpg"); inStream = getContentResolver().openInputStream(filePath); selectedImage = BitmapFacto

我正在尝试将用户选择的位图保存到我自己的应用程序路径中

不幸的是,对于非常大的图像,我得到了记忆错误

我正在使用以下代码:

private String loadImage (Uri filePath) {
    File fOut = new File(getFilesDir(),"own.jpg");

    inStream = getContentResolver().openInputStream(filePath);
    selectedImage = BitmapFactory.decodeStream(inStream);

    selectedImage.compress(CompressFormat.JPEG, 100, new FileOutputStream(fOut));
}
我有没有办法将任何大小的Uri图像文件保存到一个文件中

*我无法调整图像大小,例如使用calculateInSampleSize方法

我有没有办法将任何大小的Uri图像文件保存到一个文件中

由于它已经是一个映像,只需将字节从
InputStream
复制到
OutputStream

private void copyInputStreamToFile( InputStream in, File file ) {
    try {
        FileOutputStream out = new FileOutputStream(file);
        byte[] buf = new byte[8192];
        int len;

        while((len=in.read(buf))>0){
            out.write(buf,0,len);
        }

        out.flush();
        out.getFD().sync();
        out.close();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
(改编自)

我有没有办法将任何大小的Uri图像文件保存到一个文件中

由于它已经是一个映像,只需将字节从
InputStream
复制到
OutputStream

private void copyInputStreamToFile( InputStream in, File file ) {
    try {
        FileOutputStream out = new FileOutputStream(file);
        byte[] buf = new byte[8192];
        int len;

        while((len=in.read(buf))>0){
            out.write(buf,0,len);
        }

        out.flush();
        out.getFD().sync();
        out.close();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

(改编自)

这不适合我。。。没有错误,只是没有文件!我的Uri(文件路径)指向本地文件。有什么想法吗?@user1148358:使用调试器逐步检查代码。如果代码正在将大量字节写入所需的
文件
,则该文件显然存在。这对我不起作用。。。没有错误,只是没有文件!我的Uri(文件路径)指向本地文件。有什么想法吗?@user1148358:使用调试器逐步检查代码。如果代码正在将大量字节写入所需的
文件
,则该文件显然存在。