我想下载一个文件到storage/0/emulated/download。如果我们在应用程序启动时使用 Permission.storage.request() 授予权限,则工作正常,但如果我们在运行时通过 Permission.storage.request() 单击“允许”来授予权限,则会出现权限被拒绝的错误。
最佳答案
您应该将文件下载到您的应用程序自己的(沙盒)存储空间
private void saveAppPrivateFils(Bitmap bitmap) {
File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "2.jpg");
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(file));
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
bos.flush();
bos.close();
Toast.makeText(MainActivity.this, "success", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "fail" + e.toString(), Toast.LENGTH_SHORT).show();
}
}
关于android - 即使授予了权限,访问 'storage/0/emulated/download' 路径时也被拒绝权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71704095/