关于BitmapFactory.decodeFileDescriptor return null

开发逗萁上遇到很奇怪的BUG

public Bitmap decodeSampledBitmapFromFileDescriptor(FileDescriptor fd, int reqWidth, int reqHeight) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFileDescriptor(fd, null, options);
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFileDescriptor(fd, null, options);
}

最后返回那一句
return BitmapFactory.decodeFileDescriptor(fd, null, options);
配置高的手机上面没有问题,但是配置低的手机如HM NOTE 1LTE上面时常返回null,百思不得其解。(但是真机调试时又是正常的,不知道是不是调试的时候内存没限制)
最后使用了BitmapFactory.decodeFile解决了

public Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) {    
    final BitmapFactory.Options options = new BitmapFactory.Options();    
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(path, options);   
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);   
    options.inJustDecodeBounds = false;    
    return BitmapFactory.decodeFile(path, options);
}

这样就算在HM NOTE 1LTE这种配置低的机子上也不会返回null

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,208评论 25 708
  • 2021期待与你一起共事,点击查看岗位[https://www.jianshu.com/p/6f4d67fa406...
    闲庭阅读 16,712评论 0 75
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,908评论 18 139
  • 嘿嘿嘿,这个清明节可以说突破了很多个第一。 第一次吃椰子。哈哈,天真的以为椰子里是像平时喝的椰奶一样的颜色呢,原来...
    梦霞mm阅读 459评论 0 0
  • 旅行在路上,常常要问路。而有些人的指路,真的不是在帮你;不信你看我在深圳与河南的几次问路经历。 1...
    横笛云海阅读 635评论 15 7