Reputation: 41
private void triggerStorageAccessFramework() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
startActivityForResult(intent, REQUEST_CODE_STORAGE_ACCESS);
}
@Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_CODE_STORAGE_ACCESS) {
if (resultCode == Activity.RESULT_OK) {
// Get Uri from Storage Access Framework.
Uri treeUri = data.getData();
// Persist access permissions.
final int takeFlags = data.getFlags()
& (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
getContentResolver().takePersistableUriPermission(treeUri, takeFlags);
DocumentFile pickedDir = DocumentFile.fromTreeUri(DocumentTreeActivity.this, treeUri);
DocumentFile rootDir = pickedDir.findFile("Images");
if (rootDir == null || !rootDir.exists()) {
rootDir = pickedDir.createDirectory("Images");
}
} }
I can create the folder,i can read & write the files from/to the folder. Later, after running the cleaner (inbuilt, Redmi note 8 pro) app, the folder and files got deleted. I doubt the folder is detecting as residual . Anybody please help. Forgive my bad english
Upvotes: 1
Views: 227
Reputation: 41
I solved the issue by changing the name of the folder with a name other than application name. I don't know the logic/trick behind. But it works.
Upvotes: 0