简体   繁体   English

将图像文件写入 sdcard 时出现“Permission Denied”错误

[英]“Permission Denied” error when write image file into sdcard

I was using a demo project to acquire and save image into SDcard, it works well.我正在使用一个演示项目来获取图像并将其保存到 SD 卡中,效果很好。 But after creating a new project with the same code, it keeps giving "Permission Denied" error and IO exception.但是在使用相同的代码创建一个新项目后,它不断给出“Permission Denied”错误和 IO 异常。

The xml and gradle files are copied from the demo project and only several activities are deleted, the permissions are stated in the xml file xml 和 gradle 文件是从演示项目中复制的,仅删除了几个活动,权限在 xml 文件中说明

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

Also dynamically requested in the code也在代码中动态请求

public class PermissionManager {
    private static final int REQUEST_CODE_ASK_PERMISSIONS = 1;
    private static final String[] PERMISSIONS_ARRAYS = new String[] {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE};
    private static List<String> permissionsList = new ArrayList<>();
    private PermissionManager() {
    }
    public static void onResume(final Activity activity) {
        boolean isHasPermission = true;
        for (String permission : PERMISSIONS_ARRAYS) {
            if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
                isHasPermission = false;
                break;
            }
        }
        if (!isHasPermission) {
            for (String permission : PERMISSIONS_ARRAYS) {
                if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
                    permissionsList.add(permission);
                }
            }
            ActivityCompat.requestPermissions(activity,
                    permissionsList.toArray(new String[permissionsList.size()]), REQUEST_CODE_ASK_PERMISSIONS);
        }
    }

I found there are several questions about this issue but as shown above, the permission request and statement are all set and they work well in the old project, could anyone give some hint?我发现关于这个问题有几个问题,但如上所示,权限请求和声明都已设置,并且它们在旧项目中运行良好,有人可以提供一些提示吗?

There's a new feature for devices running on Android 10+ when you are handling media files and probably caused your problem.当您处理媒体文件并可能导致您的问题时,在 Android 10+ 上运行的设备有一个新功能。 For Android 10, you can temperarily opt-out the scoped storage by adding this line to the manitest file:对于 Android 10,您可以通过将此行添加到 manitest 文件来临时选择退出范围存储:

 <application android:requestLegacyExternalStorage="true" ... >

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM