bitmapfactory - D/skia: --- SkAndroidCodec::NewFromStream 返回 null
问题描述
我正在尝试从中调整位图的大小,并且在工作流程中我遇到此异常阻止了我。
这有什么问题?
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
关于这个问题的任何提示?我正在使用安卓 8
解决方案
对于面临相同问题的任何人,以下解决了它。
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);
推荐阅读
- reactjs - 有什么方法可以解决这个错误,每次单击 mui 数据表时,在 StrictMode 中不推荐使用 findDOMNode 的错误?
- node.js - Nodejs exceljs输出文件不正确
- centos - centos: 出现错误名为 no module lnamed “_repo”
- pandas - 过滤 Pandas DataFrame 以仅显示包含字符串列表中所有字符串的行
- javascript - 用于输出数据的 PHP 和用于读取数据的 AJAX
- python - 如何将员工信息从一本字典复制到另一本字典?
- redis - 具有复杂模式匹配的 Redis Pub/Sub
- django - Django过滤列表中的确切值
- apache-spark - 由于火花偏斜,胶水作业失败
- matrix - 更快地计算距离矩阵