当前位置 : 主页 > 手机开发 > android >

android – 通过尝试保存位图图像,始终获得“权限被拒绝”或“没有此类文件或

来源:互联网 收集:自由互联 发布时间:2021-06-11
我试图通过以下代码保存位图图像: File sdcard = Environment.getExternalStorageDirectory(); String filename = "test"; File folder = new File(sdcard, "/Download"); Log.v("ImageStorage1", "EXiST?: " + folder.exists()); folder.mk
我试图通过以下代码保存位图图像:

File sdcard = Environment.getExternalStorageDirectory();
            String filename = "test";
            File folder = new File(sdcard, "/Download");
            Log.v("ImageStorage1", "EXiST?: " + folder.exists());
            folder.mkdirs();
            Log.v("ImageStorage2", "EXIST!: " + folder.exists());
            Log.v("ImageStorage", "Folder: " + folder);
            File file = new File(folder, filename + ".jpg");

            try {
                FileOutputStream out = new FileOutputStream(file.getAbsoluteFile());
                result.compress(Bitmap.CompressFormat.JPEG, 90, out);
                out.flush();
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

我也在清单文件中使用:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

但是我得到了这个:

V/ImageStorage1: EXiST?: true
V/ImageStorage2: EXIST!: true
W/System.err: java.io.FileNotFoundException: 
/storage/emulated/0/Download/test.jpg (Permission denied)
    W/System.err:     at java.io.FileOutputStream.open0(Native Method)
    W/System.err:     at java.io.FileOutputStream.open(FileOutputStream.java:287)
    W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:223)
    W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:171)

实际上,我的任务是存储到另一个文件夹,当我使用它时:

File folder = new File(sdcard, "/kpi/test/a");

我得到了

V/ImageStorage1: EXiST?: false
V/ImageStorage2: EXIST!: false
(No such file or directory)

即使:

folder.mkdirs();

我尝试了很多并且冲浪了很多,但是没有找到答案:(

runtime permissions让用户在运行时允许或拒绝任何权限.使用这个lib Dexter库.另外检查一个工作的例子 here

在build.gradle中包含库

dependencies{
    implementation 'com.karumi:dexter:4.2.0'
}

此示例请求WRITE_EXTERNAL_STORAGE.

Dexter.withActivity(this)
                .withPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .withListener(new PermissionListener() {
                    @Override
                    public void onPermissionGranted(PermissionGrantedResponse response) {
                        // permission is granted, open the camera
                    }

                    @Override
                    public void onPermissionDenied(PermissionDeniedResponse response) {
                        // check for permanent denial of permission
                        if (response.isPermanentlyDenied()) {
                            // navigate user to app settings
                        }
                    }

                    @Override
                    public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {
                        token.continuePermissionRequest();
                    }
                }).check();

请求多个权限
要同时请求多个权限,可以使用withPermissions()方法.下面的代码请求STORAGE和LOCATION权限.

Dexter.withActivity(this)
                .withPermissions(
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.ACCESS_FINE_LOCATION)
                .withListener(new MultiplePermissionsListener() {
                    @Override
                    public void onPermissionsChecked(MultiplePermissionsReport report) {
                        // check if all permissions are granted
                        if (report.areAllPermissionsGranted()) {
                            // do you work now
                        }

                        // check for permanent denial of any permission
                        if (report.isAnyPermissionPermanentlyDenied()) {
                            // permission is denied permenantly, navigate user to app settings
                        }
                    }

                    @Override
                    public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {
                        token.continuePermissionRequest();
                    }
                })
                .onSameThread()
                .check();
网友评论