写一个从uri load图片到imageView的功能,在从uri读取文件进行重新decode的时候出现了这个错误,困惑了半天,发现原来是inputStream引起的
public static Bitmap decodeUri(Context context, Uri uri, int reqWidth, int reqHeight) {
InputStream input = null;
Bitmap bitmap = null;
try {
input = context.getContentResolver().openInputStream(uri);
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(input, null, o);
input.close();
input = null;
o.inSampleSize = calculateInSampleSize(o, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
o.inJustDecodeBounds = false;
input = context.getContentResolver().openInputStream(uri);// 这个地方需要重新获取inputstream,不然就会出现如上错
bitmap = BitmapFactory.decodeStream(input, null, o);
input.close();
input = null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
try {
if(input != null) {
input.close();
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
return bitmap;
}网上有说inputStream.markSupported() 和 inputStream.reset() 搭配使用解决的,但是我没有成功。
本文详细介绍了在使用Android从URI读取文件进行图片重新解码时遇到的问题,以及如何通过重新获取InputStream来解决错误。提供了代码示例,并讨论了使用InputStream.markSupported()与reset()的方法,但作者分享的是另一种成功解决该问题的方法。
1534

被折叠的 条评论
为什么被折叠?



