我在我的安卓应用中使用下载管理器来下载图片。
为了获取“WRITE_EXTERNAL_STORAGE”权限,我使用Dexter库。
Dexter.withContext(getApplicationContext())
.withPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
.withListener(new PermissionListener() {
@Override
public void onPermissionGranted(PermissionGrantedResponse permissionGrantedResponse) {
Toast.makeText(LiveTaskActivity.this, "Granted", Toast.LENGTH_SHORT).show();
}
@Override
public void onPermissionDenied(PermissionDeniedResponse permissionDeniedResponse) {
Toast.makeText(LiveTaskActivity.this, "Denied", Toast.LENGTH_SHORT).show();
}
@Override
public void onPermissionRationaleShouldBeShown(PermissionRequest permissionRequest, PermissionToken permissionToken) {
}
}).check();
但是每次我手动接受应用程序的授权权限申请后,仍然显示“拒绝”的权限提示。我也尝试手动实现权限请求代码。
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
} else {
download();
}
但是,我无法获得许可。这是 Manifest 代码:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:requestLegacyExternalStorage="true"
这是我的build.gradle(app)代码:
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "app.gigg.me.app"
minSdkVersion 22
targetSdkVersion 30
versionCode 2
versionName '1.0.1'
请帮忙解决这个问题。
**更新:这是我的下载代码:
private void startDownload() {
Toast.makeText(this, "Downloading File...", Toast.LENGTH_SHORT).show();
fileName = link.substring(link.lastIndexOf("/") + 1) + ".png";
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath(), fileName);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(link))
.setTitle(fileName)
.setDescription("Downloading")
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setDestinationUri(Uri.fromFile(file))
.setMimeType("image/*")
.setAllowedOverMetered(true)
.setAllowedOverRoaming(true);
download_id = downloadManager.enqueue(request);
}
neither user nor current process has android.permission.WRITE_EXTERNAL_STORAGE
。你能给我一些参考资料吗? - kish0n