359

I am getting

open failed: EACCES (Permission denied)

on the line OutputStream myOutput = new FileOutputStream(outFileName);

I checked the root, and I tried android.permission.WRITE_EXTERNAL_STORAGE.

How can I fix this problem?

try {
    InputStream myInput;

    myInput = getAssets().open("XXX.db");

    // Path to the just created empty db
    String outFileName = "/data/data/XX/databases/"
            + "XXX.db";

    // Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    // Transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }

    // Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();
    buffer = null;
    outFileName = null;
}
catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
Randika Vishman
  • 7,157
  • 3
  • 55
  • 75
Mert
  • 5,728
  • 6
  • 28
  • 60
  • I had the same problem on an Android tablet, but my situation may be unique, so I report my solution here in a comment. The error occurred when the app tried to access directory /data/data/myapp/foo which had over 50 xml files. The app did not clean the folder due to a bug. After some old files were deleted, the error disappeared. I do not know why. It could be a problem of the generic Android device. – Hong Mar 22 '14 at 01:43
  • This solved me : https://stackoverflow.com/questions/33666071/android-marshmallow-request-permission – partho Jun 28 '18 at 18:00

34 Answers34

352

For API 23+ you need to request the read/write permissions even if they are already in your manifest.

// Storage Permissions
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE
};

/**
 * Checks if the app has permission to write to device storage
 *
 * If the app does not has permission then the user will be prompted to grant permissions
 *
 * @param activity
 */
public static void verifyStoragePermissions(Activity activity) {
    // Check if we have write permission
    int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (permission != PackageManager.PERMISSION_GRANTED) {
        // We don't have permission so prompt the user
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
        );
    }
}

AndroidManifest.xml

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

For official documentation about requesting permissions for API 23+, check https://developer.android.com/training/permissions/requesting.html

Raphael Royer-Rivard
  • 2,160
  • 1
  • 27
  • 51
Justin Fiedler
  • 5,928
  • 3
  • 17
  • 22
  • 13
    Just to clarify, in order for this to work, the activity must handle the activity permissions request response. See http://developer.android.com/training/permissions/requesting.html#handle-response for more details. – kldavis4 Dec 04 '15 at 19:31
  • Handy link with explanation and examples here http://inthecheesefactory.com/blog/things-you-need-to-know-about-android-m-permission-developer-edition/en – Paul Alexander Dec 08 '15 at 14:16
  • 1
    isn't this a bad thing? with respect to user experience. – M. Usman Khan Dec 11 '15 at 12:03
  • @usman wrt user experience, it is meant to be a good thing, since the user will be able to grant a single permission every time instead that all togheter, and the user would have a better understanding of what the application is doing on their phone. It will instead be a bad thing for user experience because the user will accept every permission request nevertheless, without even caring. – ocramot Mar 17 '16 at 21:50
  • 5
    But where do I use it? for example, if I want to take a picture, do I have to run verifyStoragePermissions before startActivityForResult, or at onActivityResult? it really confuses me. – Kiwi Lee Apr 04 '16 at 11:07
  • @Kiwi Lee you must use it before you decode your image. ie before using decodeFile(filepath) method – Ashwin Shirva Jun 18 '16 at 11:13
  • 4
    Thanks this solve my problem. just a note: if someone can't resolve "Manifest.permission" you just need to import "import android.Manifest". – Oubaida AlQuraan Jul 12 '16 at 09:56
  • You do not need to add READ_EXTERNAL_STORAGE, as WRITE_EXTERNAL_STORAGE counts as both. Source from official documentation https://developer.android.com/training/basics/data-storage/files.html#GetWritePermission – Raphael Royer-Rivard Oct 24 '16 at 01:10
  • @kldavis4 this is not required now, just request permission – Nikmoon Jul 07 '20 at 22:26
337

Google has a new feature on Android Q: filtered view for external storage. A quick fix for that is to add this code in the AndroidManifest.xml file:

<manifest ... >
    <!-- This attribute is "false" by default on apps targeting Android Q. -->
    <application android:requestLegacyExternalStorage="true" ... >
     ...
    </application>
</manifest>

You can read more about it here: https://developer.android.com/training/data-storage/compatibility

Uriel Frankel
  • 12,370
  • 7
  • 41
  • 62
248

I had the same problem... The <uses-permission was in the wrong place. This is right:

 <manifest>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
        ...
        <application>
            ...
            <activity> 
                ...
            </activity>
        </application>
    </manifest> 

The uses-permission tag needs to be outside the application tag.

Community
  • 1
  • 1
user462990
  • 5,162
  • 3
  • 31
  • 33
  • 2
    it is that the uses-permission needs to be outside the application – user462990 Mar 28 '12 at 12:35
  • 3
    I get a warning: "`` tag appears after `` tag" – mr5 Aug 18 '14 at 06:52
  • 12
    WARNING In android 4.4.4 do not use the parameter `android:maxSdkVersion="18"`. It was generating this exception – guisantogui Aug 26 '14 at 22:39
  • @guisantogui, thank you! You save me a lot of hours of debugging while I don't understand while my app doesn't have the permission to write to the external storage. – andrei_zaitcev Dec 04 '14 at 00:57
  • @guisantogui same job for me, but on Android 5.0.1. I was following the official [tutorial](http://developer.android.com/training/camera/photobasics.html) which explicitly says to include `android:maxSdkVersion="18"` for android 4.4 onwards, but obviously that isn't true – rbennett485 Apr 16 '15 at 14:11
  • 1
    This permission is enforced starting in API level 19. Before API level 19, this permission is not enforced and all apps still have access to read from external storage. – AndroidGeek Jul 01 '15 at 08:15
  • 1
    i m using API lvl 15 , i m getting error while attempting to move file to OTG USB Stick – Ravi Mehta Sep 24 '15 at 08:22
  • Very hard to find error unless you look at the log cat. It's a bit confusing if you 're using `BitmapFactory.decodeFile()` as is stays silent about any errors – peterchaula Dec 12 '16 at 09:56
  • I'm also facing the same problem and in my code, it is already placed there still getting the same error. The problem is with Android 10 when your app targeting API level 29. – Sujeet Sep 24 '20 at 07:19
  • 1
    After API 29 you will need this: https://medium.com/@sriramaripirala/android-10-open-failed-eacces-permission-denied-da8b630a89df – Carlos Barcellos Dec 01 '20 at 20:09
67

Add android:requestLegacyExternalStorage="true" to the Android Manifest It's worked with Android 10 (Q) at SDK 29+
or After migrating Android X.

 <application
    android:name=".MyApplication"
    android:allowBackup="true"
    android:hardwareAccelerated="true"
    android:icon=""
    android:label=""
    android:largeHeap="true"
    android:supportsRtl=""
    android:theme=""
    android:requestLegacyExternalStorage="true">
rhaldar
  • 951
  • 9
  • 6
58

I have observed this once when running the application inside the emulator. In the emulator settings, you need to specify the size of external storage ("SD Card") properly. By default, the "external storage" field is empty, and that probably means there is no such device and EACCES is thrown even if permissions are granted in the manifest.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Audrius Meskauskas
  • 18,378
  • 9
  • 63
  • 78
53

In addition to all the answers, make sure you're not using your phone as a USB storage.

I was having the same problem on HTC Sensation on USB storage mode enabled. I can still debug/run the app, but I can't save to external storage.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
john
  • 1,262
  • 1
  • 17
  • 28
  • Thanks man, Even though i had requested permissions from user at runtime. This error occurred on emulator. – Sharp Edge Sep 11 '16 at 22:57
  • 1
    When I connect the Samsung Galaxy S6 device it first gives an option "Allow access to device data" with "DENY" or "ALLOW" and also from notification bar I am getting an option Use USB for 1. MTP 2. PTP 3. MIDI Devices 4. Charging. Which one to choose? – testsingh Nov 13 '16 at 19:58
43

Solution for Android Q:

<application ...
    android:requestLegacyExternalStorage="true" ... >
mubin986
  • 1,227
  • 8
  • 15
  • You've save my day. Permission is granted but Glide cannot read images until I set this. – leegor Nov 22 '19 at 14:39
  • 3
    Caution: Android 11 will completely remove legacy access in August 2020, so better upgrade file access to use Storage Access Framework (SAF) for your apps – dimib Apr 29 '20 at 16:20
  • It's well documented here: https://developer.android.com/guide/topics/providers/document-provider – dimib Aug 25 '20 at 09:30
30

My issue was with "TargetApi(23)" which is needed if your minSdkVersion is bellow 23.

So, I have request permission with the following snippet

protected boolean shouldAskPermissions() {
    return (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1);
}

@TargetApi(23)
protected void askPermissions() {
    String[] permissions = {
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE"
    };
    int requestCode = 200;
    requestPermissions(permissions, requestCode);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
// ...
    if (shouldAskPermissions()) {
        askPermissions();
    }
}
Piroxiljin
  • 529
  • 5
  • 11
27

Be aware that the solution:

<application ...
    android:requestLegacyExternalStorage="true" ... >

Is temporary, sooner or later your app should be migrated to use Scoped Storage.

In Android 10, you can use the suggested solution to bypass the system restrictions, but in Android 11 (R) it is mandatory to use scoped storage, and your app might break if you kept using the old logic!

This video might be a good help.

omzer
  • 378
  • 3
  • 9
19

I'm experiencing the same. What I found is that if you go to Settings -> Application Manager -> Your App -> Permissions -> Enable Storage, it solves the issue.

Atul Kaushik
  • 5,033
  • 3
  • 25
  • 32
14

It turned out, it was a stupid mistake since I had my phone still connected to the desktop PC and didn't realize this.

So I had to turn off the USB connection and everything worked fine.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Tobias Reich
  • 4,414
  • 2
  • 42
  • 77
  • worked for me as well. But why does it work? what difference does not connecting USB to PC make. – user13107 May 09 '17 at 06:35
  • Good question. I never investigated this any further. I'd guess there is a write lock from the system the moment you connect the device (so it may emulate a usb device on your computer). Maybe this is in order to avoid inconsistent data. Not sure though. – Tobias Reich May 09 '17 at 08:12
  • I found the reason here http://stackoverflow.com/questions/7396757/programmatically-turn-off-usb-storage-on-android-devices also see my answer http://stackoverflow.com/a/43863548/1729501 – user13107 May 09 '17 at 08:21
14

Android 10 (API 29) introduces Scoped Storage. Changing your manifest to request legacy storage is not a long-term solution.

I fixed the issue when I replaced my previous instances of Environment.getExternalStorageDirectory() (which is deprecated with API 29) with context.getExternalFilesDir(null).

Note that context.getExternalFilesDir(type) can return null if the storage location isn't available, so be sure to check that whenever you're checking if you have external permissions.

Read more here.

jacoballenwood
  • 2,050
  • 1
  • 19
  • 33
13

I had the same problem on Samsung Galaxy Note 3, running CM 12.1. The issue for me was that i had

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

and had to use it to take and store user photos. When I tried to load those same photos in ImageLoader i got the (Permission denied) error. The solution was to explicitly add

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

since the above permission only limits the write permission up to API version 18, and with it the read permission.

ZooS
  • 588
  • 7
  • 15
9

In addition to all answers, if the clients are using Android 6.0, Android added new permission model for (Marshmallow).

Trick: If you are targeting version 22 or below, your application will request all permissions at install time just as it would on any device running an OS below Marshmallow. If you are trying on the emulator then from android 6.0 onwards you need to explicitly go the settings->apps-> YOURAPP -> permissions and change the permission if you have given any.

Nourdine Alouane
  • 763
  • 11
  • 19
9

Strangely after putting a slash "/" before my newFile my problem was solved. I changed this:

File myFile= new File(Environment.getExternalStorageDirectory() + "newFile");

to this:

File myFile= new File(Environment.getExternalStorageDirectory() + "/newFile");

UPDATE: as mentioned in the comments, the right way to do this is:

File myFile= new File(Environment.getExternalStorageDirectory(), "newFile");
Darush
  • 9,625
  • 7
  • 53
  • 53
  • 2
    Nothing strange here. With the first line you are trying to create a file in the same directory as the one that contains your external storage directory. ie /storage/.../somethingnewfile instead of /storage/.../something/newfile – Stéphane May 31 '17 at 07:46
  • @Stéphane The error itself is strange if you look at the broad range of answers. – Darush May 31 '17 at 08:54
  • 4
    The correct way to do this is to use the File(dir, file) cosntructor – Nick Cardoso Feb 20 '18 at 08:29
  • @NickCardoso You can also use File(String pathname). Looks like you are not getting the point here. The question is not about how to use the File class. The range of answers in this thread shows how misleading this error is and how many possible ways of resolving it. In my case the error was the missing slash. Some people have found it useful. Sure you could use your own method of accessing a file. Go ahead and post your answer. It may solves someone's problem. – Darush Feb 20 '18 at 08:39
  • *You* are not getting the point. If you use the correct constructor, then your hack is not required. My comment was added so it can help any future readers misled by your mistake. It wasn't a personal criticism and didn't need a reply – Nick Cardoso Feb 20 '18 at 08:58
  • @NickCardoso you could have suggested an edit instead of posting a comment to prevent readers being mislead by my mistake. You are still confused. If a person used the correct way, as pointed by you, he wouldn't come to this thread at first. I am just suggesting the possible cause of that error and how to solve it. – Darush Feb 20 '18 at 09:09
  • And they would be coming here to find the correct way. Editing your answer so they never see your mistake wouldn't teach anything. – Nick Cardoso Feb 20 '18 at 09:35
  • @NickCardoso There is no such thing as absolute correct way. The answers in a thread are just some alternate ways to resolve an issue. – Darush Feb 20 '18 at 10:04
  • In the case of path construction, there absolutely is a correct way, or an unreliable hack. This conversation is ironic given your profile claims. – Nick Cardoso Feb 20 '18 at 11:11
  • 1
    I would like to backup @NickCardoso... When I used File(pathname) constructor I got error... With File(dir, file) constructor everything works... – Brontes Aug 05 '20 at 12:10
6

I had the same problem and none of suggestions helped. But I found an interesting reason for that, on a physical device, Galaxy Tab.

When USB storage is on, external storage read and write permissions don't have any effect. Just turn off USB storage, and with the correct permissions, you'll have the problem solved.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Hamlet Kraskian
  • 416
  • 6
  • 9
  • 2
    Damn it, I just spent like 3 hours working around. Aditionally, I had to reboot the device and it did work. Thank you so much – crgarridos Oct 03 '16 at 15:10
5

Change a permission property in your /system/etc/permission/platform.xml
and group need to mentioned as like below.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
    <group android:gid="sdcard_rw" />
    <group android:gid="media_rw" />    
</uses-permission>
Prabakaran
  • 128
  • 1
  • 9
5

I had the same error when was trying to write an image in DCIM/camera folder on Galaxy S5 (android 6.0.1) and I figured out that only this folder is restricted. I simply could write into DCIM/any folder but not in camera. This should be brand based restriction/customization.

Mahdi Mehrizi
  • 210
  • 3
  • 8
5

Maybe the answer is this:

on the API >= 23 devices, if you install app (the app is not system app), you should check the storage permission in "Setting - applications", there is permission list for every app, you should check it on! try

Jason Zhu
  • 73
  • 1
  • 6
5

I would expect everything below /data to belong to "internal storage". You should, however, be able to write to /sdcard.

ovenror
  • 502
  • 4
  • 11
  • 5
    I tried too. still same. – Mert Jan 13 '12 at 17:32
  • 1
    The /data-partition is generally write-protected to ordinary (non-root) users trying to to ordinary file access. For internal storage, there are specialized methods to do so, especially if you want to access databases. The [Android developer pages](http://developer.android.com/guide/topics/data/data-storage.html) should help you on this issue. – ovenror Jan 13 '12 at 17:37
4

When your application belongs to the system application, it can't access the SD card.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
will
  • 41
  • 1
4

keep in mind that even if you set all the correct permissions in the manifest: The only place 3rd party apps are allowed to write on your external card are "their own directories" (i.e. /sdcard/Android/data/) trying to write to anywhere else: you will get exception: EACCES (Permission denied)

Elad
  • 1,035
  • 9
  • 7
4
Environment.getExternalStoragePublicDirectory();

When using this deprecated method from Android 29 onwards you will receive the same error:

java.io.FileNotFoundException: open failed: EACCES (Permission denied)

Resolution here:

getExternalStoragePublicDirectory deprecated in Android Q

user2965003
  • 306
  • 2
  • 11
3

In my case I was using a file picker library which returned the path to external storage but it started from /root/. And even with the WRITE_EXTERNAL_STORAGE permission granted at runtime I still got error EACCES (Permission denied).
So use Environment.getExternalStorageDirectory() to get the correct path to external storage.

Example:
Cannot write: /root/storage/emulated/0/newfile.txt
Can write: /storage/emulated/0/newfile.txt

boolean externalStorageWritable = isExternalStorageWritable();
File file = new File(filePath);
boolean canWrite = file.canWrite();
boolean isFile = file.isFile();
long usableSpace = file.getUsableSpace();

Log.d(TAG, "externalStorageWritable: " + externalStorageWritable);
Log.d(TAG, "filePath: " + filePath);
Log.d(TAG, "canWrite: " + canWrite);
Log.d(TAG, "isFile: " + isFile);
Log.d(TAG, "usableSpace: " + usableSpace);

/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    }
    return false;
}

Output 1:

externalStorageWritable: true
filePath: /root/storage/emulated/0/newfile.txt
isFile: false
usableSpace: 0

Output 2:

externalStorageWritable: true
filePath: /storage/emulated/0/newfile.txt
isFile: true
usableSpace: 1331007488
vovahost
  • 25,072
  • 12
  • 95
  • 99
  • I was having the same exact problem. Removing the `root` part from the path resolved my issue. – xabush Nov 30 '17 at 16:29
2

I am creating a folder under /data/ in my init.rc (mucking around with the aosp on Nexus 7) and had exactly this problem.

It turned out that giving the folder rw (666) permission was not sufficient and it had to be rwx (777) then it all worked!

lane
  • 585
  • 13
  • 17
2

The post 6.0 enforcement of storage permissions can be bypassed if you have a rooted device via these adb commands:

root@msm8996:/ # getenforce
getenforce
Enforcing
root@msm8996:/ # setenforce 0
setenforce 0
root@msm8996:/ # getenforce
getenforce
Permissive
Sergey Brunov
  • 11,755
  • 7
  • 39
  • 71
Zakir
  • 2,054
  • 18
  • 29
2

To store a file in a directory which is foreign to the app's directory is restricted above API 29+. So to generate a new file or to create a new file use your application directory like this :-

So the correct approach is :-

val file = File(appContext.applicationInfo.dataDir + File.separator + "anyRandomFileName/")

You can write any data into this generated file !

The above file is accessible and would not throw any exception because it resides in your own developed app's directory.

The other option is android:requestLegacyExternalStorage="true" in manifest application tag as suggested by Uriel but its not a permanent solution !

Santanu Sur
  • 8,836
  • 7
  • 24
  • 43
1

Add Permission in manifest.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Harman Khera
  • 218
  • 2
  • 8
0

I had the same problem (API >= 23).

The solution https://stackoverflow.com/a/13569364/1729501 worked for me, but it was not practical to disconnect app for debugging.

my solution was to install proper adb device driver on Windows. The google USB driver did not work for my device.

STEP 1: Download adb drivers for your device brand.

STEP 2: Go to device manager -> other devices -> look for entries with word "adb" -> select Update driver -> give location in step 1

Community
  • 1
  • 1
user13107
  • 2,719
  • 4
  • 29
  • 48
0

Add gradle dependencies

implementation 'com.karumi:dexter:4.2.0'

Add below code in your main activity.

import com.karumi.dexter.Dexter;
import com.karumi.dexter.MultiplePermissionsReport;
import com.karumi.dexter.PermissionToken;
import com.karumi.dexter.listener.PermissionRequest;
import com.karumi.dexter.listener.multi.MultiplePermissionsListener;
    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {


                checkMermission();
            }
        }, 4000);
    }

    private void checkMermission(){
        Dexter.withActivity(this)
                .withPermissions(
                        android.Manifest.permission.READ_EXTERNAL_STORAGE,
                        android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        android.Manifest.permission.ACCESS_NETWORK_STATE,
                        Manifest.permission.INTERNET
                ).withListener(new MultiplePermissionsListener() {
            @Override
            public void onPermissionsChecked(MultiplePermissionsReport report) {
                if (report.isAnyPermissionPermanentlyDenied()){
                    checkMermission();
                } else if (report.areAllPermissionsGranted()){
                    // copy some things
                } else {
                    checkMermission();
                }

            }
            @Override
            public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {
                token.continuePermissionRequest();
            }
        }).check();
    }
ethemsulan
  • 2,113
  • 24
  • 19
0

Building on answer by user462990

To be notified when the user responds to the permission request dialog, use this: (code in kotlin)

override fun onRequestPermissionsResult(requestCode: Int,
                                        permissions: Array<String>,
                                        grantResults: IntArray) {
    when (requestCode) {
        MY_PERMISSIONS_REQUEST_READ_CONTACTS -> {
            // If request is cancelled, the result arrays are empty.
            if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.

            }
            return
        }

        // Add other 'when' lines to check for other
        // permissions this app might request.

        else -> {
                // Ignore all other requests.
        }   
    }
}
Vanguard3000
  • 225
  • 1
  • 6
  • 14
user3561494
  • 1,695
  • 1
  • 14
  • 31
0

In my case the error was appearing on the line

      target.createNewFile();

since I could not create a new file on the sd card,so I had to use the DocumentFile approach.

      documentFile.createFile(mime, target.getName());

For the above question the problem may be solved with this approach,

    fos=context.getContentResolver().openOutputStream(documentFile.getUri());

See this thread too, How to use the new SD card access API presented for Android 5.0 (Lollipop)?

Tarasantan
  • 629
  • 6
  • 6
0

For android 11 Permission

Declare MANAGE_EXTERNAL_STORAGE permission in manifest.xml

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

in code check permission of manage storage

 if (Environment.isExternalStorageManager()==false) {
     Uri uri = Uri.parse("package:" + BuildConfig.APPLICATION_ID);
     startActivity(new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION, uri));
} else {
   // TO do.. open file picker intent
}

all other file permission code must be same as code below android 11 version

Manjeet Brar
  • 894
  • 1
  • 10
  • 27
-2

after adding permission solved my problem

<uses-permission android:name="android.permission.INTERNET"/>
Sai Gopi Me
  • 9,800
  • 1
  • 65
  • 43