Bitmap.BitmapFactory.decodeFile(path) 为空

2019-08-16 12:40:25来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

Bitmap.BitmapFactory.decodeFile(path) 为空

错误描述:

  String path=photoPath + photoPathimage;
   if (new File(path).exists()) {
       bm = BitmapFactory.decodeFile(path);
     }
   Bitmap take = ResizeBitmap(bm, 300);
   v_addimage.setImageBitmap(take);
   bm.recycle();//太大记得回收

判断文件存在,但是bm一直返回null

网上解决办法  增加文件读写权限

private void getPermission() {
        PackageManager pm = getPackageManager();
        boolean permission = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.CAMERA", "com.zhengyuan.learningtakephoto"));
        if (!permission) {
            //没有权限
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(new String[]{Manifest.permission.CAMERA}, 15);
            }
        }
        permission = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.READ_EXTERNAL_STORAGE", "com.zhengyuan.learningtakephoto"));
        if (!permission) {
            //没有权限
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 15);
            }
        }
        permission = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.WRITE_EXTERNAL_STORAGE", "com.zhengyuan.learningtakephoto"));
        if (!permission) {
            //没有权限
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 15);
            }
        }
    }

试了下 好像并不怎么行。
使用如下动态授权权限,进入会提示用户是否授权写入权限,确认就可以操作文件了。
//TODO 设置读写权限
        if (Build.VERSION.SDK_INT >= 23) {
            int REQUEST_CODE_CONTACT = 101;
            String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
            //验证是否许可权限
            for (String str : permissions) {
                if (this.checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) {
                    //申请权限
                    this.requestPermissions(permissions, REQUEST_CODE_CONTACT);
                }
            }
        }

 





原文链接:https://www.cnblogs.com/Jack-chenz/p/11363946.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Springboot 2启动内置Tomcat源码分析

下一篇:20 个案例教你在 Java 8 中如何处理日期和时间?