Android 10 open file exception open failed: eacces (permission denied) android:requestLegacyExternalStorage= “true“

in Android development, we generally use the following code to get the storage path, and the results are generally /storage/emulated/0

in AndroidManifest. Application node in the XML file with the android: requestLegacyExternalStorage = “true” attribute, as follows: </ p>

<application
    android:requestLegacyExternalStorage="true"

may be easy for developers, but for those who are new to android, it doesn’t use

now most mobile phones are Android version 10.0, when reading and writing files to the memory card, the problem of: open failed: EACCES (Permission denied) will occur. After adding the above one sentence of code to baidu, it has not been solved. The possible reason is that there is no dynamic access Permission added in the code snippet

dynamic read and write permission code:

final int REQUEST_EXTERNAL_STORAGE = 1;
if (ContextCompat.checkSelfPermission(MainActivity.this,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 101);
                } else {
                    //downloadFile();
                    /**
                     * Write the code you want to execute here
                     */
                }

Where is

used?

for example, we now have a button, we get its click event in the Activity, click we want to add a photo in: /storage/emulated/0/DCIM, we want to perform the operation in else can

example:

no dynamic permissions added:

 utils_get.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getDataFromokHttpUtils();
            }
        });

add dynamic permissions:

utils_down.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final int REQUEST_EXTERNAL_STORAGE = 1;
                String[] PERMISSIONS_STORAGE = {
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE };
                if (ContextCompat.checkSelfPermission(MainActivity.this,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 101);
                } else {
                    downloadFile();
                }

            }
        });

</ div>

Read More: