使用DownloadManager下载PDF文件

时间:2018-12-17 19:22:21

标签: android android-download-manager

我正在尝试从服务器下载PDF文件。

外部存储已保存:

  

静态最终文件fSdcard =新   File(Environment.getExternalStorageDirectory()。toString(),“ VDTEC”);

文件名:

  

静态最终文件fManualMult =新文件(fSdcard,“ ManualMult.pdf”);

下载ID

  

私人长lDownloadID;

URL:

  

静态最终字符串sUrlManualMult =“ http://germipasto.com.br/comunicacao/app/MULT/MANUAL%20VD%20TEC%20MULT%20-%20revis%C3%A3o%2001%20-%2002-fev%202018.pdf”;

当我按下按钮时,“ beginDownload”方法称为:

  

private void beginDownload(){

    /*
    Create a DownloadManager.Request with all the information necessary to start the download
     */

    DownloadManager.Request request=new DownloadManager.Request(Uri.parse(sUrlManualMult))
            .setMimeType("application/pdf")
            .setTitle("Dummy File")// Title of the Download Notification
            .setDescription("Downloading")// Description of the Download Notification
            .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)// Visibility of the download Notification
            .setDestinationUri(Uri.fromFile(fManualMult))// Uri of the destination file
            .setRequiresCharging(false)// Set if charging is required to begin the download
            .setAllowedOverMetered(true)// Set if download is allowed on Mobile network
            .setAllowedOverRoaming(true);// Set if download is allowed on roaming network
    request.allowScanningByMediaScanner();
    DownloadManager downloadManager= (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    lDownloadID = downloadManager.enqueue(request);// enqueue puts the download request in the queue.
}

我还有一个广播公司,它在下载完成后显示敬酒:

  

private BroadcastReceiver onDownloadComplete = new BroadcastReceiver()   {

    @Override
    public void onReceive(Context context, Intent intent) {
        //Fetching the download id received with the broadcast
        long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
        //Checking if the received broadcast is for our enqueued download by matching download id
        if (lDownloadID == id) {
            Toast.makeText(DocumentosActivity.this, "Download Completed", Toast.LENGTH_SHORT).show();
        }
    }
};

并注册:

  

registerReceiver(onDownloadComplete,new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

问题是: 当我单击时,将在1-2秒内显示Toast,并且下载未完成。 我在做什么错了?

PS:我发现,如果更改链接(例如:直接使用http://speedtest.ftp.otenet.gr/files/test10Mb.db),则downloadmanager可以工作。 我的链接可能有错误? 我通过ftp上传了文件,可以通过浏览器访问它。

0 个答案:

没有答案