【问题标题】:Android 10: BitmapFactory.decodeFileDescriptor returns nullAndroid 10:BitmapFactory.decodeFileDescriptor 返回 null
【发布时间】:2020-04-21 09:27:16
【问题描述】:

在运行 Android 10 的模拟器上,当我在 FireDescriptor 上使用 decodeFileDescriptor 时,如下所示,位图创建良好。但是当我尝试设置选项然后使用该选项对象获取位图时,选项对象似乎没有正确设置并且位图为空。请让我知道我做错了什么。感谢您的帮助。

Bitmap test1c = BitmapFactory.decodeFileDescriptor(fdCompressed);
//This works
testImgView.setImageBitmap(test1c);

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFileDescriptor(fdCompressed, null, options);
//The line above sets outHeight and outWidth of options to -1

options.inJustDecodeBounds = false;

//This bitmap object is null
Bitmap test1e = BitmapFactory.decodeFileDescriptor(fdCompressed, null, options);
testImgView.setImageBitmap(test1e);

【问题讨论】:

    标签: bitmapfactory android-10.0


    【解决方案1】:

    找到它不工作的原因。我一开始所做的测试导致了这个问题。

    Bitmap test1c = BitmapFactory.decodeFileDescriptor(fdCompressed);
    

    因为看起来 ParcelFileDescriptor 对象只能使用一次来生成位图。当我删除那条线并尝试时,它工作正常。我花了几个小时才弄清楚这一点,希望它对某人有所帮助。

    【讨论】:

      【解决方案2】:

      与问题不完全相关,但我们遇到了这个问题,因为将非空 paddingRect 传递给该方法,这只是使其返回 null 而没有提及原因。

      Bitmap test1e = BitmapFactory.decodeFileDescriptor(fdCompressed, new Rect(), options); // <-- bad
      

      改为:

      Bitmap test1e = BitmapFactory.decodeFileDescriptor(fdCompressed, null, options); // <-- good
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-22
        • 2012-05-28
        • 2014-12-05
        • 2021-07-07
        • 2013-06-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多