奇怪的权限拒绝异常(WRITE_EXTERNAL_STORAGE)

时间:2017-01-07 10:28:50

标签: android android-permissions

我的manifest.xml是这样的:

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.my.app"
          xmlns:android="http://schemas.android.com/apk/res/android">

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

    <application
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity"
                  android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

如你所见,我有:

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

但是当我在我的应用程序中按下一个按钮时会触发以下代码:

 MediaStore.Images.Media.insertImage(
    getContentResolver(), myView.getDrawingCache(),
    UUID.randomUUID().toString()+".png", "my pic");

我遇到了以下异常:

Failed to insert image
java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=2574, uid=11336 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission()
                                                                        at android.os.Parcel.readException(Parcel.java:1627)

为什么呢?我有Androidmanifest文件中定义的权限。

2 个答案:

答案 0 :(得分:1)

targetSdkVersion 22降级为^(?:\s*|\{[^}]*\})$ 或添加程序权限。见official doc

答案 1 :(得分:0)

从Android 6.0开始,在运行时请求权限,而不是在安装之前请求权限。

现在,您需要执行需要权限的操作(在您的情况下,在外部存储上写入文件),您必须这样做:

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission. WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        // Show an explanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.
    } else {
        // No explanation needed, we can request the permission.
        ActivityCompat.requestPermissions(thisActivity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);

        // MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
}

然后,覆盖``以查看您的用户是否已授予许可。

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0 && grantResults[0] ==  PackageManager.PERMISSION_GRANTED) {
                // permission was granted, yay!
            } else {
                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

您可以获得有关新权限模型here的更多信息。