android.permission.WRITE_MEDIA_STORAGE error

点点圈 提交于 2019-12-24 00:45:36

问题


I am working on an app that edits text files. I am able to r/w files when they are opened via an 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);

It all works just right on both internal and external SD storage.

I have also set up the app with an intent-filter for android.intent.action.VIEW. The app shows up in the "chooser" in the Android MyFiles application. The Uri returned from MyFiles looks different than the one returned from ACTION_OPEN_DOCUMENT, i.e. it has a file number instead of a file name. Using the Uri in intent.getData() that MyFiles passes to me I am able to read the files but not write them to the SD card (external storage). However, I am able to both read and write files to internal storage.

In my Manifest file I have the following permissions defined:

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

In the java code I'm also explicitly asking for the permissions as well:

    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)

I have a result handler for requestPermissions() and it shows that the permissions were successfully granted.

When I try to open the file for writing I get the following exception:

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

Exception: External path: /storage/0123-4567/Test/123.txt: Neither user 10196 nor current process has android.permission.WRITE_MEDIA_STORAGE.


Is there some other permission that I need to request to do the write, or is there some other technique I need to use to open the file with the full r/w access after I get the file handle from MyFiles?


回答1:


Unless you have root, the problem seems to be unfixable, see this post by 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").



来源:https://stackoverflow.com/questions/48737936/android-permission-write-media-storage-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!