gpt4 book ai didi

android - 下载文件需要什么权限?

转载 作者:太空宇宙 更新时间:2023-11-03 12:44:49 25 4
gpt4 key购买 nike

我正在尝试使用 DownloadManager 类下载文件。

public void downloadFile(View view) {

String urlString = "your_url_here";
try {
// Get file name from the url
String fileName = urlString.substring(urlString.lastIndexOf("/") + 1);
// Create Download Request object
DownloadManager.Request request = new DownloadManager.Request(Uri.parse((urlString)));
// Display download progress and status message in notification bar
request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
// Set description to display in notification
request.setDescription("Download " + fileName + " from " + urlString);
// Set title
request.setTitle("DownloadManager");
// Set destination location for the downloaded file
request.setDestinationUri(Uri.parse("file://" + Environment.getExternalStorageDirectory() + "/" + fileName));
// Download the file if the Download manager is ready
did = dManager.enqueue(request);

} catch (Exception e) {
}
}

// BroadcastReceiver to receive intent broadcast by DownloadManager
private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {

@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Query q = new Query();
q.setFilterById(did);
Cursor cursor = dManager.query(q);
if (cursor.moveToFirst()) {
String message = "";
int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
if (status == DownloadManager.STATUS_SUCCESSFUL) {
message = "Download successful";
} else if (status == DownloadManager.STATUS_FAILED) {
message = "Download failed";
}
tvMessage.setText(message);
}


}
};

我正在使用dexter获取权限

 Dexter.withActivity(this)
.withPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
.withListener(new PermissionListener() {
@Override
public void onPermissionGranted(PermissionGrantedResponse response) {

我的 list 中也有这两个

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

但我在尝试下载文件时仍然遇到此错误(仅在 Oreo 上)。它适用于安卓 7

No permission to write to /storage/emulated/0/download: Neither user 10205 nor current process has android.permission.WRITE_EXTERNAL_STORAGE.

最佳答案

您只需要互联网许可。

<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

如果你想存储和阅读这个下载的文件。

关于android - 下载文件需要什么权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49589552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com