android.permission.WRITE_MEDIA_STORAGE 错误

标签 android android-permissions android-external-storage

我正在开发一个编辑文本文件的应用程序。当通过 ACTION_OPEN_DOCUMENT Activity 打开文件时,我可以读写文件:

 Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
 intent.addCategory(Intent.CATEGORY_OPENABLE);
 intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

这一切都适用于内部和外部 SD 存储。

我还为应用程序设置了一个用于 android.intent.action.VIEW 的 intent-filter。该应用程序显示在 Android MyFiles 应用程序的“选择器”中。从 MyFiles 返回的 Uri 看起来与从 ACTION_OPEN_DOCUMENT 返回的不同,即它有一个文件号而不是文件名。 使用 MyFiles 传递给我的 intent.getData() 中的 Uri,我能够读取文件但不能将它们写入 SD 卡(外部存储)。但是,我能够读取文件和将文件写入内部存储。

在我的 list 文件中,我定义了以下权限:

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

在 java 代码中,我也明确要求权限:

    int permis = CheckSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);

            if (permis ==  PackageManager.PERMISSION_GRANTED)
            {
                Toast.makeText(getApplicationContext(),"Permits Manifest.permission.WRITE_EXTERNAL_STORAGE", Toast.LENGTH_LONG);
                return true;
            }

            Toast.makeText(getApplicationContext(),"Getting Manifest.permission.WRITE_EXTERNAL_STORAGE", Toast.LENGTH_LONG);

            //android.permission.WRITE_MEDIA_STORAGE

            String[] array = {  Manifest.permission.READ_EXTERNAL_STORAGE,
                                Manifest.permission.WRITE_EXTERNAL_STORAGE
                    };

   ActivityCompat.requestPermissions(thisActivity,array, MY_PERMISSIONS_REQUEST_READ_CONTACTS)

我有一个 requestPermissions() 的结果处理程序,它显示权限已成功授予。

当我尝试打开文件进行写入时,出现以下异常:

 pfd = MainActivity.this.getContentResolver().openFileDescriptor(uri, "w");
 fileOutputStream = new FileOutputStream(pfd.getFileDescriptor());

异常:外部路径:/storage/0123-4567/Test/123.txt:用户10196和当前进程都没有android.permission.WRITE_MEDIA_STORAGE。


在我从 MyFiles 获取文件句柄后,我是否需要请求其他一些权限才能进行写入,或者是否需要使用其他一些技术来打开具有完整读/写访问权限的文件?

最佳答案

除非您有 root,否则问题似乎无法解决,请参阅 this Jorrit“Chainfire”Jongma 发表的文章:

So, if you have purchased an Android device that came preloaded with 16gb (or 8, or 32, etc) storage and it can also use an SD card, your device has two external storages already.

The internal flash memory would usually be considered the primary external storage location, while the SD card would be considered the secondary external storage location (if internal flash is present). A USB stick or harddrive you can potentially connect would also be secondary external storage.

It appears that in newer Android builds, Google is making it impossible for third party apps (apps that were not preloaded on the device, but you manually installed or downloaded from Android Market / Google Play) to gain write access to the external SD card.

[...]

In the past, an app would request the "WRITE_EXTERNAL_STORAGE" permission, which would grant write access to all external storages (user/group "sdcard_rw"). This has apparently been changed to only grant write access to the primary external storage. A second permission has been introduced called "WRITE_MEDIA_STORAGE", which would grant access to the other external storages (user/group "media_rw").

关于android.permission.WRITE_MEDIA_STORAGE 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48737936/

相关文章:

Android Studio 找不到导入的库

java - 如何在Android模拟器上向WAMP服务器发出HTTP请求

java - 如何检查 fragment 中的权限

android - 使应用程序与平板电脑兼容

android - 无法在 Android/data 中创建文件夹

java - 如何保存图片并在电脑上访问它?

android如何在jpeg上创建鱼眼效果

java - 更新 Runnable 中的 TextView 会导致 Activity 停止

java - 使用 android.content.Context.checkPermission 检查权限时出现空指针异常

android - 无法访问辅助存储(外部可移动微型 SD 卡)