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?