Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is READ_EXTERNAL_STORAGE permission ever required to read Uri from Intent.ACTION_GET_CONTENT

Tags:

android

I was wondering, if I launch the following Intent.ACTION_GET_CONTENT

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("application/zip");
startActivityForResult(intent, RequestCode.REQUEST_CHOOSE_BACKUP_FILE);

and try to read the returned Uri from intent in the following way.

Uri uri = data.getData();

// Figure out extension
ContentResolver contentResolver = getContext().getContentResolver();
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
final String extension = mimeTypeMap.getExtensionFromMimeType(contentResolver.getType(uri));

File temp = null;
try {
    temp = File.createTempFile(Utils.getJStockUUID(), "." + extension);
} catch (IOException e) {
    e.printStackTrace();
}

// Delete temp file when program exits.
temp.deleteOnExit();

InputStream inputStream = null;
OutputStream outputStream = null;

try {
    inputStream = getContext().getContentResolver().openInputStream(uri);
    outputStream = new FileOutputStream(temp);

    byte[] buffer = new byte[8 * 1024];
    int bytesRead;
    while ((bytesRead = inputStream.read(buffer)) != -1) {
        outputStream.write(buffer, 0, bytesRead);
    }
} catch (IOException e) {
    Log.e(TAG, "", e);
} finally {
    close(outputStream);
    close(inputStream);
}

Is READ_EXTERNAL_STORAGE permission ever required?

I tested a few round. To my surprise, I can perform success read without request for READ_EXTERNAL_STORAGE.

I just would like to confirm READ_EXTERNAL_STORAGE isn't really required to read Uri from Intent.ACTION_GET_CONTENT, in all type of situation.

like image 677
Cheok Yan Cheng Avatar asked Oct 17 '22 17:10

Cheok Yan Cheng


1 Answers

I have had instances where a user had a third-party file manager installed (File Manager+) and in those cases reading from the Uri returned by ACTION_GET_CONTENT would fail with a permission error if the READ_EXTERNAL_STORAGE permission was not first granted (only if they used the third-party app to select the file, if they used Google Drive or the normal system selection it worked fine without the permission).

I was able to replicate the behavior by installing File Manager+ on one of my emulators with the Play Store and trying it out.

like image 66
Tyler V Avatar answered Nov 15 '22 06:11

Tyler V