-
Notifications
You must be signed in to change notification settings - Fork 28
Open
Description
On Android 11+ (API 30+) app won't work without scoped storage access.
Possible solution
`
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK && requestCode == RQS_OPEN_DOCUMENT_TREE){
Uri uriTree = data.getData();
assert uriTree != null;
DocumentFile documentFile = DocumentFile.fromTreeUri(this, uriTree);
assert documentFile != null;
String fileName = "";
for (DocumentFile file : documentFile.listFiles()) {
DocumentFile directory = DocumentFile.fromTreeUri(this, file.getUri());
if (!file.isDirectory()) {
Cursor cursor = this.getContentResolver().query(file.getUri(), null, null, null, null);
assert cursor != null;
int columnIndex = cursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME);
if (cursor.moveToFirst()) {
fileName = cursor.getString(columnIndex);
}
cursor.close();
File appExternalDir = new File(this.getExternalFilesDir(null), "/TextFiction/games/" + fileName);
try (InputStream in = getContentResolver().openInputStream(file.getUri())) {
try (FileOutputStream out = new FileOutputStream(appExternalDir)) {
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}//Files.copy(src, appExternalDir.toPath());//API 26
}
}
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.mi_access: {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE );//_TREE
startActivityForResult(intent, RQS_OPEN_DOCUMENT_TREE );
return true;
}
}
return false;
}
`
Metadata
Metadata
Assignees
Labels
No labels