我正在尝试从此处调整位图大小,并且在工作流程中我有这个异常阻止我。
这有什么问题?
public Bitmap decodeSampledBitmapFromUri(Uri fileUri, int reqWidth, int reqHeight) {
InputStream stream = null;
try {stream = new BufferedInputStream(mApplicationContext.getContentResolver().openInputStream(fileUri)); } catch (FileNotFoundException e) {Log.e("TAG","InputStream stream is null");}
try { stream.mark(stream.available()); } catch (IOException e) {Log.e("TAG","Thrown IOException in stream.mark(stream.available()");}
if (stream == null) {Log.e("TAG","stream is null");}
BitmapFactory.Options options = new BitmapFactory.Options();
// First decode with inJustDecodeBounds=true to check dimensions
options.inJustDecodeBounds = true;
try {stream.reset(); } catch (IOException e) {Log.e("TAG","Thrown IOException in stream.reset() 1");}
BitmapFactory.decodeStream(stream, null, options);
try {stream.reset(); } catch (IOException e) {Log.e("TAG","Thrown IOException in stream.reset() 2");}
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
try {stream.reset(); } catch (IOException e) {Log.e("TAG","Thrown IOException in stream.reset() 3");}
BitmapFactory.decodeStream(stream, null, options);
// Decode bitmap with inSampleSize set
try {stream.reset(); } catch (IOException e) {Log.e("TAG","Thrown IOException in stream.reset() 4");}
return BitmapFactory.decodeStream(stream, null, options);
}
05-16 16:31:14.621 com.example.xxx E/TAG: Thrown IOException in stream.reset() 4
05-16 16:31:14.621 com.example.xxx D/skia: --- SkAndroidCodec::NewFromStream returned null
有关此问题的任何提示吗?我使用的是Android 8
答案 0 :(得分:0)
对于任何面对同样的人,以下解决了它。
byte[] imageData = null;
InputStream is = null;
try { is = new BufferedInputStream(mApplicationContext.getContentResolver().openInputStream(fileUri)); } catch (FileNotFoundException e) {Log.e("TAG","FILEnotFoundException");}
try { imageData = IOUtils.toByteArray(is); } catch (IOException e) {Log.e("TAG","imagedata IOException");}
try { is.close(); } catch (IOException e) {Log.e("TAG","is.close() IOException");}
// First decode with inJustDecodeBounds=true to check dimensions
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(imageData, 0, imageData.length, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap reducedBitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.length, options);
return Bitmap.createScaledBitmap(reducedBitmap, reqWidth, reqHeight, false);