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);
                }
            }
        }

 




posted on 2019-08-16 14:59  新禧  阅读(4867)  评论(0编辑  收藏  举报

导航