I need to access all files and folders , that user can normally access, because in app scenario user should pick a folder to download files. And my app stucked into endless loop by starting settings activity. I'm tried to use
requestIntent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION, Uri.parse("package:" + BuildConfig.APPLICATION_ID));
if(hasPerms == true){
}else {
intentActivityResultLauncher.launch(requestIntent);
}
private ActivityResultLauncher<Intent> intentActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult o) {
if (o.getResultCode() == RESULT_OK) {
//...
}else{
//...
}
}
});
According to settings UI of 12 android, i'm already granted permission. But if i try to check granted permission ,i'll get false.
hasPerms = checkSelfPermission(Manifest.permission.MANAGE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
Log.i(TAG, "onStart: "+hasPerms);
hasPerms = checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
Log.i(TAG, "onStart: "+hasPerms);
hasPerms = checkSelfPermission(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION) == PackageManager.PERMISSION_GRANTED;
Log.i(TAG, "onStart: "+hasPerms);
All Log.i methods will print false in logcat.
I already declared my permissions in manifest:
<manifest xmlns:androi="http://schemas.android.com/apk/distribution"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" tools:ignore="ScopedStorage"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
If i try to request permission using Manifest.MANAGE_EXTERNAL_STORAGE
i'll get activity not found exception.
After some switching on my brain , i came to that solution:
I can check granted permission using SharedPrefernes
instead of checkSelfPermission
method. So, i just puted in cache String with value "true"
when result code is ok. Then simply chech it when launches activity.
Create an intent to request manage all files permission.
Intent requestIntent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
Get a SharedPrefences instance
SharedPreferences cache = getSharedPreferences("cache", Context.MODE_PRIVATE);
Create a result launcher, in method onActivityResult
if result is ok then simply put String with value in cache using SharedPrefernes.Editor
private ActivityResultLauncher<Intent> intentActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult o) {
if(o.getResultCode() == RESULT_OK) {
cache.edit().putString("perms","true").commit();
//...
}else {
//...
}
}
});
Then check permission by checking string value in app's cache.
if(cache.getString("perms","false").equals("false")) {
intentActivityResultLauncher.launch(requestIntent);
}