如何从Uri加载采样图像?

问题描述 投票:1回答:1

我需要以较低的分辨率加载图像。我该怎么办?

我累了下一个代码:

public Bitmap decodeSampledBitmapFromUri(@NonNull Uri Uri, int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    try {

        InputStream is = getContentResolver().openInputStream(Uri);

        BitmapFactory.decodeStream(is, null, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);   // I can read width and height from options

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;

        return BitmapFactory.decodeStream(is, null, options);
    } catch (Exception ignored)
    {

    }
    return null;
}

而且我什至可以看到加载图像的宽度和高度。但是当我将BitmapFactory.decodeStream设为false时,null返回inJustDecodeBounds。怎么了?

用户从图库中选择的图像。授予所有权限。

java android bitmap
1个回答
1
投票

您的InputStream被第一个decodeStream()关闭。打开一个新的InputStream以与第二个decodeStream()一起使用。

© www.soinside.com 2019 - 2024. All rights reserved.