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