【发布时间】:2018-02-21 12:17:38
【问题描述】:
我已经在 android 应用中创建了我的网站的 webview。 我在 Android 中使用下载管理器从我的网站下载 PDF 文件。但是没有 PDF 文件的直接链接,而是生成了 PDF 文件,因此,我无法下载该文件。下载显示不成功。
我的编码:
myWebView.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
try{
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));
if(isExternalStorageWritable() && isExternalStorageReadable()) {
request.setTitle("Invoice");
request.setDescription("Downloads");
request.setVisibleInDownloadsUi(true);
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
request.setDestinationInExternalFilesDir(MainActivity.this,Environment.DIRECTORY_DOWNLOADS, "invoice.pdf");//download to internal memory
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(), "Downloading File", //To notify the Client that the file is being downloaded
Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(getApplicationContext(), "No SD Card Present.."+Environment.DIRECTORY_DOWNLOADS, //To notify the Client that the file is being downloaded
Toast.LENGTH_LONG).show();
}
}
catch (Exception e)
{
Log.d("Caught inside uri", e.toString());
}
}
});// checks for downloads
【问题讨论】:
-
“但是没有 PDF 文件的直接链接,而是生成了 PDF 文件”——如果你提供一个 URL 来演示你正在做什么,你会有更好的运气。
-
假设这是我的网址:example.com/en/module/mpsellerinvoice/… 那么这个网址会在网站中生成 PDF 文件,但不会在 android 的 webview 中生成
标签: android pdf android-download-manager