我需要什么权限才能下载文件? [英] What permissions do I need to download files?

查看:856
本文介绍了我需要什么权限才能下载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to download a file using the DownloadManager class.

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) {

我的清单中也都包含了

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

但是尝试下载文件时仍然出现此错误(仅在Oreo上).它适用于android 7

But I still get this error while trying to download files (ONLY on Oreo). It works on android 7

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

推荐答案

您只需要Internet许可.

You only need internet permission.

<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" />

如果要存储和读取此下载文件.

if you want to store and read this downloaded file.

这篇关于我需要什么权限才能下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆