从 Intent.ACTION_GET_CONTENT 读取 Uri 是否需要 READ_EXTERNAL_STORAGE 权限

Che*_*eng 11 android

我想知道,如果我启动以下Intent.ACTION_GET_CONTENT

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("application/zip");
startActivityForResult(intent, RequestCode.REQUEST_CHOOSE_BACKUP_FILE);
Run Code Online (Sandbox Code Playgroud)

并尝试通过以下方式读取意图返回的 Uri。

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);
}
Run Code Online (Sandbox Code Playgroud)

是否READ_EXTERNAL_STORAGE需要许可?

我测试了几轮。令我惊讶的是,我可以在没有请求的情况下执行成功读取READ_EXTERNAL_STORAGE

我只是想确认在所有类型的情况下READ_EXTERNAL_STORAGE并不真正需要从 读取 Uri 。Intent.ACTION_GET_CONTENT

Tyl*_*r V 5

我遇到过用户安装了第三方文件管理器(File Manager+)的情况,在这些情况下,如果未首先授予 READ_EXTERNAL_STORAGE 权限(仅当他们使用第三方应用程序来选择文件,如果他们使用 Google Drive 或正常的系统选择,则无需许可即可正常工作)。

我可以通过在我的一个模拟器上使用 Play 商店安装 File Manager+ 并尝试来复制该行为。

  • 对我来说,即使使用提到​​的文件管理器+应用程序,一切都可以在没有读取权限的情况下运行,但有些用户报告了 SecurityException 崩溃,所以我将尝试这个解决方案。但这确实很奇怪。 (2认同)