无法使用DownloadManager下载文件

时间:2017-09-19 07:15:06

标签: android android-download-manager

我目前正在尝试使用Android应用中的DownloadManager类进行下载。以下是我的代码:

DownloadHelper.Class

public class DownloadHelper {
    DownloadManager downloadManager;
    Context context;
    public DownloadHelper(Context context){
        this.context = context;
    }
    public long downloadData (Uri uri,String fileName,String mimeType) {

        long downloadReference;

        downloadManager = (DownloadManager)context.getSystemService(DOWNLOAD_SERVICE);
        DownloadManager.Request request = new DownloadManager.Request(uri);
        request.setTitle(context.getString(R.string.downloading_file));
        request.setDescription(fileName);
        request.setMimeType(mimeType);
        request.setVisibleInDownloadsUi(true);
        request.allowScanningByMediaScanner();

        request.setDestinationInExternalFilesDir(context,Environment.DIRECTORY_DOWNLOADS,fileName);
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI
                | DownloadManager.Request.NETWORK_MOBILE);
        downloadReference = downloadManager.enqueue(request);


        return downloadReference;
    }
}

我在我的自定义适配器类中调用它,如下所示:

private void downloadFile(String url,String fileName,String mimeType) {
        Uri uri = Uri.parse(url);
        DownloadHelper  downloadHelper = new DownloadHelper(mContext);
        downloadHelper.downloadData(uri,fileName,mimeType);
}

问题是每当我调用downloadFile方法时,它会在状态栏上显示下载图标不到1秒,然后它就会消失。我检查我的下载文件夹,它说下载文件。关注我的是文件相对较小(1.5kb)并且它一直在显示下载文件。也许是针对这个问题的解决方案感谢

这是我的DatabaseUtils.dumpCursor()结果:

0{
status=194
_id=5270
09-19 15:12:11.198 13580-13580/com.example.project I/System.out:    local_filename=null
09-19 15:12:11.198 13580-13580/com.example.project I/System.out:    mediaprovider_uri=null
09-19 15:12:11.198 13580-13580/com.example.project I/System.out:    destination=4
09-19 15:12:11.198 13580-13580/com.example.project I/System.out:    title=Downloading file
09-19 15:12:11.198 13580-13580/com.example.project I/System.out:    description=ic_bullet.png
09-19 15:12:11.199 13580-13580/com.example.project I/System.out:    hint=file:///storage/emulated/0/Android/data/com.example.project/files/Download/ic_bullet.png
09-19 15:12:11.199 13580-13580/com.example.project I/System.out:    media_type=image/png
09-19 15:12:11.199 13580-13580/com.example.project I/System.out:    total_size=-1
09-19 15:12:11.199 13580-13580/com.example.project I/System.out:    last_modified_timestamp=1505808726864
09-19 15:12:11.199 13580-13580/com.example.project I/System.out:    bytes_so_far=0
09-19 15:12:11.199 13580-13580/com.example.project I/System.out:    allow_write=0
09-19 15:12:11.199 13580-13580/com.example.project I/System.out:    local_uri=null
09-19 15:12:11.199 13580-13580/com.example.project I/System.out:    reason=placeholder
}

2 个答案:

答案 0 :(得分:0)

您是否考虑过在单独的线程上运行downloadFile(而不是在UI线程上)?

答案 1 :(得分:0)

您是否在Manifest

中添加了互联网权限?
  <uses-permission android:name="android.permission.INTERNET" />