三星Galaxy Api 23版本6上Android的WRITE_EXTERNAL_STORAGE权限问题

时间:2019-01-28 18:04:11

标签: android pdf android-permissions

我需要使用uri打开pdf。 我有打开pdf的代码。 我的代码在大于23的Api上可以正常工作。 当我尝试在具有Api 23和版本6的Samsung Galaxy设备上下载文件时,收到以下SecurityException。

这里是例外:

01-25 19:28:38.723 6655-6667/android.process.media E/DatabaseUtils: Writing exception to parcel
    java.lang.SecurityException: No permission to write to /storage/emulated/0/Download/Canto Quick Start Guide.pdf: Neither user 10022 nor current process has android.permission.WRITE_EXTERNAL_STORAGE.
        at android.app.ContextImpl.enforce(ContextImpl.java:1438)
        at android.app.ContextImpl.enforceCallingOrSelfPermission(ContextImpl.java:1470)
        at android.content.ContextWrapper.enforceCallingOrSelfPermission(ContextWrapper.java:675)
        at com.android.providers.downloads.DownloadProvider.checkFileUriDestination(DownloadProvider.java:724)
        at com.android.providers.downloads.DownloadProvider.insert(DownloadProvider.java:558)
        at android.content.ContentProvider$Transport.insert(ContentProvider.java:263)
        at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:163)
        at android.os.Binder.execTransact(Binder.java:507)

我已在清单文件中设置了所需的权限

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

我进行了以下检查,以测试用户是否没有WRITE_EXTERNAL_STORAGE权限,然后要求用户授予访问权限。

int PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE = 2;
    //             Check whether this app has write external storage permission or not.
                int writeExternalStoragePermission = ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE);
                // If do not grant write external storage permission.
                if (writeExternalStoragePermission != PackageManager.PERMISSION_GRANTED) {
                    // Request user to grant write external storage permission.
                    ((MainActivity)activity).getSharedPreferenceManager().add(PreferenceConstants.SHAREPOINT_FILE_URI, getUrl((SharepointFile) object, activity));
                    ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PermissionConstants.PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE);
                } else {
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(getUrl((SharepointFile) object, activity)));
                    intent.setDataAndType(Uri.parse(getUrl((SharepointFile) object, activity)), "application/pdf");
                    activity.startActivity(intent);

                }

我在MainActivity.java中覆盖了onRequestPermissionResult,以便一旦用户授予许可,便可以开始下载。

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    switch (requestCode) {
        case PermissionConstants.PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE:
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                String fileUri = getSharedPreferenceManager().get(PreferenceConstants.SHAREPOINT_FILE_URI);

                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(fileUri));

                startActivity(intent);
            } else {
                getSharedPreferenceManager().remove(PreferenceConstants.SHAREPOINT_FILE_URI);
            }
            break;
    }
}

我有要求用户许可的代码。我确实获得了对话框/警报以允许访问。用户单击允许后,我看到它仍然给出上述例外。我进入设置以检查我的应用程序的权限,并发现它已“打开”以进行存储。为什么仍会出现权限问题?关于我在这里可能会缺少的任何指示吗?感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

这只是一个假设,但我看到您已经声明了

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

但是docs说:

  

任何声明WRITE_EXTERNAL_STORAGE权限的应用都被隐式授予(READ_EXTERNAL_STORAGE)权限。

尝试删除

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