简体   繁体   English

Android:设备不接受WRITE_EXTERNAL_STORAGE权限

[英]Android: WRITE_EXTERNAL_STORAGE permission not accepted by device

I'm having a rather strange problem with an Android app I'm writing. 我正在编写的Android应用程序存在一个非常奇怪的问题。

In my app, I use an intent to tell the phone's camera app to take a picture for me. 在我的应用程序中,我意图告诉手机的相机应用程序为我拍照。 After I take this picture, I want to register the newly taken photo with the device's MediaStore content provider in order to make it show up in the regular "gallery" app. 拍摄完这张照片后,我想在设备的MediaStore内容提供商中注册新拍摄的照片,以使其显示在常规的“图库”应用中。

To do this, I know I need the WRITE_EXTERNAL_STORAGE permission, but even when it's in my manifest I get a permission denied exception. 为此,我知道我需要WRITE_EXTERNAL_STORAGE权限,但是即使它在清单中,我也会收到一个权限被拒绝的异常。 What do I have to do to get this permission? 我必须怎么做才能获得此许可? I'm running Android 6.0.1. 我正在运行Android 6.0.1。

Here's my permissions XML in the manifest: 这是清单中我的权限XML:

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

Here's the code that causes the exception: 这是导致异常的代码:

       // insert photo into phone's main photo content provider
        ContentResolver cr = getActivity().getContentResolver();

        try {
            MediaStore.Images.Media.insertImage(cr, photoFile.getPath(),
                    "Image Capture", "Custom Image capture");
        } catch (FileNotFoundException foe) {
            Log.e(DEBUG, foe.getMessage());
        }

And here's the exception I get: 这是我得到的例外:

java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=2436, uid=10041 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission()

EDIT : I should probably mention: 编辑 :我可能应该提到:

Right now my app uses getExternalFilesDir(Environment.DIRECTORY_PICTURES) , which returns the path /sdcard/Android/data/<package>/files/Pictures to save it's photos. 现在,我的应用程序使用getExternalFilesDir(Environment.DIRECTORY_PICTURES) ,它返回路径/sdcard/Android/data/<package>/files/Pictures来保存照片。 I want them to go where the camera app normally puts them, in /sdcard/DCIM/Camera . 我希望他们去相机应用程序通常放在/sdcard/DCIM/Camera

You should add the android:maxSdkVersion="18" to the WRITE_EXTERNAL_STORAGE as well. 您还应该将android:maxSdkVersion="18"到WRITE_EXTERNAL_STORAGE中。 Your code should look like this: 您的代码应如下所示:

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

This may be a problem with respect to you requesting the permission but not being granted the permission by the user. 关于您请求许可但未被用户授予许可的问题。 Have a look at my answer @ https://stackoverflow.com/a/35574084/529691 看看我的答案@ https://stackoverflow.com/a/35574084/529691

And see if that helps. 看看是否有帮助。

You should promt the user to provide you permission: 您应该提示用户向您提供权限:

    int externalPermissionCheck = ContextCompat.checkSelfPermission(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if (externalPermissionCheck==-1){
        askPermissionStorage();
    }


private void askPermissionStorage() {
    //for media
    if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.
            WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

        //insert explanation
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            //if you want to explaian to the user

        } else {//if no explanation needed
            ActivityCompat.requestPermissions(this, new
                            String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
        }
    }
}

The above is for save the picture\\video and this is for the camera permission: 上面是用于保存图片\\视频,这是用于摄像机权限的:

    int cameraPermissionCheck = ContextCompat.checkSelfPermission(this,
            Manifest.permission.CAMERA);
    if (cameraPermissionCheck == -1) {
        askPermissionCamera();
    }
private void askPermissionCamera() {
    //for camera
    if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.
            CAMERA) != PackageManager.PERMISSION_GRANTED) {
        //insert explanation
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.CAMERA)) {
            //if you want to explaian to the user

        } else {//if no explanation needed
            ActivityCompat.requestPermissions(this, new
                            String[]{Manifest.permission.CAMERA},
                    MY_PERMISSIONS_REQUEST_CAMERA);
        }
    }
}

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

相关问题 WRITE_EXTERNAL_STORAGE权限 - WRITE_EXTERNAL_STORAGE Permission android:installLocation =“ preferExternal”和WRITE_EXTERNAL_STORAGE权限 - android:installLocation=“preferExternal” and WRITE_EXTERNAL_STORAGE permission WRITE_EXTERNAL_STORAGE 权限不在 Android 中询问 13 - WRITE_EXTERNAL_STORAGE permission Not asking in Android 13 Android-WRITE_EXTERNAL_STORAGE权限并编写应用日志 - Android - WRITE_EXTERNAL_STORAGE permission and writing app's log android - 如果用户未授予 WRITE_EXTERNAL_STORAGE 权限,则关闭应用程序 - android - close app if user not granting WRITE_EXTERNAL_STORAGE permission Android中的Gdx.files.local和WRITE_EXTERNAL_STORAGE权限 - Gdx.files.local and WRITE_EXTERNAL_STORAGE permission in android 在没有 WRITE_EXTERNAL_STORAGE 权限的情况下在 Android 8 上使用相机 - Use Camera on Android 8 without WRITE_EXTERNAL_STORAGE permission 遇到“运行时” Android WRITE_EXTERNAL_STORAGE权限 - Struggling with a 'run-time' Android WRITE_EXTERNAL_STORAGE permission 未显示WRITE_EXTERNAL_STORAGE的权限对话框 - Permission dialog is not shown for WRITE_EXTERNAL_STORAGE Android 4.4:WRITE_EXTERNAL_STORAGE权限对主外部存储/ mnt / sdcard有效吗? - Android 4.4: is WRITE_EXTERNAL_STORAGE permission effective on primary external storage /mnt/sdcard?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM