首页 > 解决方案 > 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

标签: bitmapfactory

解决方案


对于面临相同问题的任何人,以下解决了它。

    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);

推荐阅读