我在想,如果我启动下面的Intent.ACTION_GET_CONTENT
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("application/zip");
startActivityForResult(intent, RequestCode.REQUEST_CHOOSE_BACKUP_FILE);并尝试通过以下方式从intent读取返回的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);
}是否需要 READ_EXTERNAL_STORAGE 权限?
我测试了几轮。令我惊讶的是,我可以在不请求READ_EXTERNAL_STORAGE的情况下执行成功读取。
我只想确认一下,在所有类型的情况下,从Intent.ACTION_GET_CONTENT读取Uri都不需要READ_EXTERNAL_STORAGE。
https://stackoverflow.com/questions/50826019
复制相似问题