Writing to SD card "java.io.FileNotFoundException: Permission denied"

There are many questions on stackoverflow, but I still couldn't find a solution to my specific problem:

I have a permission app WRITE_EXTERNAL_STORAGE

and a non-export file provider

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.cgogolin.library"
      ...
      <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
      <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="23" />
      ...
      <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.cgogolin.library.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
      </provider>
</application>

      

which is further specified provider_paths.xml

as

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
  <external-path name="external_files" path="."/>
  <root-path name="external_files2" path="/storage/"/>
</paths>

      

and which I want to use to send files from the SD card to other applications (My application acts more or less like a file browser in this case, but not really ...).

Since I am using API level 23, I am also dynamically requesting permissions by calling

requestPermissions(new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE, android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, WRITE_PERMISSION_REQUEST);

      

A dialog will appear and after clicking Allow (and restart the application due to the error described in Cannot write to external storage if the application does not restart after granting permission ). The app is getting the permissions correctly, which I can check by checking,

android.support.v4.content.ContextCompat.checkSelfPermission(context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED

      

and indeed it is. I can also get the file Uri

in File

from sdcard with something like

Uri uri = android.support.v4.content.FileProvider.getUriForFile(context, "com.cgogolin.library.fileprovider", file);

      

I can read from this file and I can, for example, run successfully Intent.ACTION_VIEW

to open the file in an external application.

But: Despite having write permissions, I cannot write the file. All attempts to open OutputStream

or ParcelFileDescriptor

before Uri

using any of the following

context.getContentResolver().openOutputStream(uri, "wa");
context.getContentResolver().openFileDescriptor(uri, "wa");

      

either directly from my app, or from an app opened via Intent

will result in

java.io.FileNotFoundException: Permission denied

      

Where is my mistake? I thought that after checkSelfPermission()

telling me what I have WRITE_EXTERNAL_STORAGE

, I really have to do it.

+3


source to share


2 answers


and which I want to use to send files from SD card to other applications

Firstly, the documentation root-path

on the FileProvider

missing.



Second, you do not have read or write access to removable storage outside of specially defined locations (for example, 2nd and subsequent values ​​returned getExternalFilesDirs()

). External storage permissions have nothing to do with removable storage.

0


source


The answer is to allow the user to grant application write permissions across Intent.ACTION_OPEN_DOCUMENT_TREE

to the entire sdcard (or corresponding subdirectory) as provided in this Android SD Card Write Permission using SAF (Storage Access Framework) .



0


source







All Articles