0

Using an emulated version of the Google Pixel 5 running Android 11 API 32, I'm attempting to make a program that will move files between folders at the press of a button, as a proof of concept I have made two folders on my device, Left (/storage/emulated/0/Left/)(Variable name: inputPath) and Right. (/storage/emulated/0/Right/)(Variable name: outputPath)

Inside "Left" I have stored a blank txt document named FILE.txt (/storage/emulated/0/Left/file.txt)(Variable name: inputFile) I am then attempting to move the file between folders with the code

            //create output directory if it doesn't exist
            File dir = new File (outputPath);
            if (!dir.exists())
            {
                dir.mkdirs();
                Log.i("checking the value of mkdirs", String.valueOf(dir.mkdirs()));
            }
            in = new FileInputStream(inputPath + inputFile);
            out = new FileOutputStream(outputPath + inputFile);
            byte[] buffer = new byte[1024];
            int read;
            while ((read = in.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }
            in.close();
            in = null;
            // write the output file
            out.flush();
            out.close();
            out = null;
            // delete the original file
            new File(inputPath + inputFile).delete();

Within my AndroidManifest I have the code

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        android:maxSdkVersion="28" />
    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
    <application
     android:requestLegacyExternalStorage="true"
     ...
     </application>

Then within my onCreate I call the method

    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
            );
        }
    }

Which requests file access the first time the user opens up the app, however, even if the user grants permissions I still get the error

open failed: EACCES (Permission denied)

I've looked up relevant solutions online but those I have looked at are not applicable to android 11, please help.

Josh Hunter
  • 45
  • 1
  • 10
  • This is even worse formulated as your closed post: https://stackoverflow.com/questions/73977313/android-11-api-32-open-failed-eacces-permission-denied?noredirect=1#comment130623768_73977313 And you do nothing with advise: `But if you then use File.canRead() and File.canWrite() they will return false. Use all three before you call your move code.` – blackapps Oct 07 '22 at 10:45
  • @blackapps I know canread and canwrite will return false, I am asking how do I make them return true? Apologies for being new, I just don't see how checking for values I already know will be false will help me at all – Josh Hunter Oct 07 '22 at 10:53
  • You should have told in your new post that you had used them. And better have posted code that showed how you used them. You did not even mention it. Nice helping you.. – blackapps Oct 07 '22 at 10:57
  • Apps do not have access to arbitrary spots on external storage via filesystem APIs on Android 11+. That includes arbitrary directories in the root of external storage, such as your `Left` and `Right`. If you set up your two directories to be locations where your app can read and write (e.g., subdirectories of `getExternalFilesDir()`), then you will have better luck. – CommonsWare Oct 07 '22 at 11:03
  • @CommonsWare is there a way to grant my app access to locations such as ```left``` and ```right``` because eventually I want to move files between external drives – Josh Hunter Oct 07 '22 at 11:14
  • 1
    That would involve `MANAGE_EXTERNAL_STORAGE`, as is mentioned in the one answer. FWIW, I cover `MANAGE_EXTERNAL_STORAGE` in [this section](https://commonsware.com/R/pages/chap-scoped-007.html) of [this free book](https://commonsware.com/R). – CommonsWare Oct 07 '22 at 11:21

1 Answers1

1

To provide permission MANAGE_EXTERNAL_STORAGE

<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"

in android 11, you must provide it manually.

Try this:

val intent: Intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
intent.addCategory("android.intent.category.DEFAULT")
intent.data = Uri.parse(String.format("package:%s",applicationContext.packageName))
startActivity(intent)

Note: The Google Play store has a policy that restricts the use of the MANAGE_EXTERNAL_STORAGE permission if the app is intended to be published to the store.

ninhnau19
  • 695
  • 1
  • 3
  • 16