I don't understand why in Android 11 use of `storage\emulated...\downloads` folder can read/write some files and not others?
Solution 1:
With help from Mark in the comments above this solved my problem. Since the .db
extension is not recognizable, I had to use intent.setType("*/*")
and validate the file and extension at the resultLauncher (i.e., ActivityResultLauncher
).
https://developer.android.com/training/data-storage/shared/documents-files#open-file
private void setupImportDBButton(){
mImportDB.setOnClickListener(v -> {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
resultLauncher.launch(intent);
});
}
Included within the ActivityResultLauncher: Basics of the functionality that worked to resolve the multi-device use forced by Android 11 (API 30) requirements. The key was the use of the getContentResolver()
.
Uri uri = result.getData().getData();
String destinationPath = this.getDatabasePath(strDBName).getPath();
InputStream source = this.getContentResolver().openInputStream(uri);
OutputStream destination = this.getContentResolver().openOutputStream(Uri.fromFile(new File(destinationPath)));
DatabaseManager.copyDatabase(source, destination);