android - 使用android webview下载图片

标签 android html webview download

我想在我的 WebView 中下载图片。我使用了这样的链接标签

<a href="../Temp/Images/def.jpg" download="">Download</div></a>

这在 chrome 浏览器上运行良好,但在我的 webview 应用程序中不起作用。我已经激活了几个权限。

<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" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />

但链接仍然没有反应。如何触发下载?

编辑:

响应头:

Cache-Control:private
Connection:Close
Content-Disposition:attachment; filename=IMG_20141004_171308.jpg
Content-Length:3039432
Content-Type:image/jpeg
Date:Wed, 15 Oct 2014 12:35:57 GMT
Server:ASP.NET Development Server/10.0.0.0
X-AspNet-Version:4.0.30319
X-AspNetMvc-Version:4.0

最佳答案

尝试添加下载监听器 -

mWebView.setDownloadListener(new DownloadListener() {

    public void onDownloadStart(String url, String userAgent,
        String contentDisposition, String mimetype,
                                   long contentLength) {

            Request request = new Request(Uri.parse(url));
            request.allowScanningByMediaScanner();

                request.setNotificationVisibility(
                DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

                request.setDestinationInExternalPublicDir(
                Environment.DIRECTORY_DOWNLOADS,    //Download folder
                "download");                        //Name of file


                DownloadManager dm = (DownloadManager) getSystemService(
                DOWNLOAD_SERVICE);

                dm.enqueue(request);  

    }
});

关于android - 使用android webview下载图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26379174/

相关文章:

php - 没有在 PHP 中删除正确的文件

html - 在 div 上定位跨度

android - 如何删除 webview 中的白色背景?

java - 双击textview android

java - 如何生成随机数?

java - 显示循环读取的第一个图像

java - Android Soundboard 一次只有一种声音

javascript - 如何使用 javascript 或 jQuery 捕获颜色对话框事件?

javascript - Javascript 执行启动 chrome 而不是 webview

android - 静态对象在 Android 上保留它们的实例多长时间?