android – 通过尝试保存位图图像,始终获得“权限被拒绝”或“没有此类文件或目录”.我该怎么办?

android – 通过尝试保存位图图像,始终获得“权限被拒绝”或“没有此类文件或目录”.我该怎么办?,第1张

概述我试图通过以下代码保存位图图像: File sdcard = Environment.getExternalStorageDirectory(); String filename = "test"; File folder = new File(sdcard, "/Download"); Log.v("ImageStorage 我试图通过以下代码保存位图图像:

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?: trueV/ImageStorage2: EXIST!: trueW/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?: falseV/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();
总结

以上是内存溢出为你收集整理的android – 通过尝试保存位图图像,始终获得“权限被拒绝”或“没有此类文件或目录”.我该怎么办?全部内容,希望文章能够帮你解决android – 通过尝试保存位图图像,始终获得“权限被拒绝”或“没有此类文件或目录”.我该怎么办?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1121458.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存