-1

Hi Guys, i tried to access a textfile in the other application in my /Android/data/com.xxx.xxxxxx/files/ in internal storage using Android 11 but unfortunately in won`t. It works in Android 10 below. It has an error at BufferedReader. Here is the error "open failed: EACCES (Permission denied)". Any Answers please? Thanks in advance:)

Here is my gradle.

android {
compileSdkVersion 31
buildToolsVersion "30.0.3"

defaultConfig {
    applicationId "com.example.myapplication"
    minSdkVersion 15
    targetSdkVersion 30
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

Here is my AndroidManifest.xml

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

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

<application
    android:requestLegacyExternalStorage="true"
    android:allowBackup="false"
    android:icon="@drawable/ic_launcher_background"
    android:label="My Application"
    android:theme="@style/AppTheme">

Here is my MainActivity.java

    public void read_file(){
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        if (SDK_INT >= 23) {
            if (checkPermission()) {
                File sdcard = Environment.getExternalStorageDirectory();
                File dir = new File(sdcard.getAbsolutePath() + "/Android/data/com.xxx.xxxxxx/files/");

                if(dir.exists()) {
                    File file = new File(dir, "xxx.txt");
                    StringBuilder text = new StringBuilder();
                    try {
                        BufferedReader br = new BufferedReader(new FileReader(file));
                        String line;
                        while ((line = br.readLine()) != null) {
                            text.append(line);
                        }
                        br.close();
                    }catch (IOException e) {
                        //You'll need to add proper error handling here
                    }
                    finally {
                    }
                }
            } else {
                showPermissionDialog(); // Code for permission
            }
        }
    }
}
Pakz
  • 123
  • 2
  • 15
  • 1
    `/Android/data/com.www.wwwwww/files/` ? You mean `/storage/emulated/0/Android/data/com.www.wwwwww/files` ? You do not need any permission for your app specific path and getExternalFilesDir(null) will give it to you. Please edit your post. – blackapps Feb 11 '22 at 05:17
  • @blackapps I mean. I need to access another application folder. – Pakz Feb 11 '22 at 06:55
  • @blackapps Sorry, i forgot. Updated already. Any suggestions? – Pakz Feb 11 '22 at 07:05
  • 1
    Well as you have seen that is not allowed on 11+. And you did not even use File.exists() and File canRead() before you tried to read. – blackapps Feb 11 '22 at 07:09
  • 1
    `//You'll need to add proper error handling here` Yes. Why didnt you? The printed stacktrace in the logcat would have told you why. – blackapps Feb 11 '22 at 07:11
  • @blackapps Its functional on Android 10 below but in Android 11 it wont. – Pakz Feb 11 '22 at 07:11
  • 1
    Also that you should have told at the first line of your post. – blackapps Feb 11 '22 at 07:24
  • @blackapps Here is the Error -----> E/Error:: java.io.FileNotFoundException: /storage/emulated/0/Android/data/com.www.wwwwww/files/www.txt: open failed: EACCES (Permission denied) – Pakz Feb 11 '22 at 07:37

1 Answers1

-1

Your app can request All files access from the user by doing the following:

  • Declare the MANAGE_EXTERNAL_STORAGE permission in the manifest.
  • Use the ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION intent action to direct users to a system settings page where they can enable the following option for your app: Allow access to manage all files.

See more details at https://developer.android.com/training/data-storage/manage-all-files

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134