[英]bitmap.compress(Bitmap.CompressFormat.JPEG,100, baos) giving me java.lang.OutOfMemoryError
[英]bitmap.compress from Uri resulting in OutOfMemoryError
我正在尝试将用户选择的位图保存到我自己的App路径中。
不幸的是,对于很大的图像,我得到OutOfMemoryError错误。
我正在使用以下代码:
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();
}
}
(改编自此SO答案 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.