【问题标题】:Why decodeStream returns null when options.inSampleSize is derived rather than set为什么在派生而不是设置 options.inSampleSize 时 decodeStream 返回 null
【发布时间】:2013-02-27 06:43:37
【问题描述】:

好的,有人帮我解决这个问题。我正在使用其他线程和 android 教程推荐的 Bitmap.options 来计算 inSample 大小。以下代码导致空位图而不是缩放位图的问题

    private int determineCorrectScale(InputStream imageStream){

        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(imageStream, null, o);

        // The new size we want to scale to
        final int REQUIRED_SIZE = 100;

        // Find the correct scale value. It should be the power of 2.
        int scale = 1;
        while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE) {
            scale *= 2;
        }

        return scale;

    }
    private String saveScaledBitmapToPhone(Uri imagUri){

        InputStream imageStream;
        try {
            imageStream = getContentResolver().openInputStream(imagUri);

            int scale= determineCorrectScale(imageStream);

            BitmapFactory.Options options=new BitmapFactory.Options();
            options.inSampleSize = scale;

            Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream, null, options );

.
.
.
.
        } catch (Exception e) {
            return imagUri.toString(); //default

        }
}

yourSelectedImage 为空的问题。但是,如果我注释掉该行

  int scale= determineCorrectScale(imageStream);

并将 insampleSize 设置为 8 或 16 或任何其他固定的手动数字,然后一切正常。任何人都可以解释这种行为或如何解决它?我的感觉说这是由于创建了两个静态类的 Options 对象,但这只是一个猜测。我仍然无法修复它:( 请帮忙

【问题讨论】:

    标签: android


    【解决方案1】:

    您正在重复使用相同的数据流。要么重置它,将数据缓存在字节数组中,要么打开一个新流。

    【讨论】:

    • 哦。我从没想过重用相同的数据流是个问题。你如何重置它?
    • @Snake 或者你只是在流上调用'reset()'函数:)
    猜你喜欢
    • 1970-01-01
    • 2015-05-29
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-13
    • 1970-01-01
    相关资源
    最近更新 更多