使用android webview下载图像

zan*_*ken 2 html android download webview

我想在我的webview中下载图像.我使用了像这样的链接标签

<a href="../Temp/Images/def.jpg" download="">Download</div></a>
Run Code Online (Sandbox Code Playgroud)

哪个在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" />
Run Code Online (Sandbox Code Playgroud)

但是这个链接仍然没有反应.如何触发下载?

编辑:

响应标题:

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
Run Code Online (Sandbox Code Playgroud)

Con*_*use 5

尝试添加下载监听器-

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

    }
});
Run Code Online (Sandbox Code Playgroud)


Wil*_*oid 5

我有同样的问题.这是我如何解决它.我扩展了WebViewClient:

import java.io.File;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.DownloadManager;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.webkit.WebView;
import android.webkit.WebViewClient;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MyWebViewClient extends WebViewClient {

    private Context context;

    public MyWebViewClient(Context context) {
        this.context = context;
    }

    @SuppressLint("NewApi")
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if(url.contains(".jpg")){

            DownloadManager mdDownloadManager = (DownloadManager) context  
                    .getSystemService(Context.DOWNLOAD_SERVICE);  
            DownloadManager.Request request = new DownloadManager.Request(  
                    Uri.parse(url));  
            File destinationFile = new File(  
                    Environment.getExternalStorageDirectory(),  
                    getFileName(url));  
            request.setDescription("Downloading ...");  
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);  
            request.setDestinationUri(Uri.fromFile(destinationFile));  
            mdDownloadManager.enqueue(request);  


            return true;
        }
        return super.shouldOverrideUrlLoading(view, url);
    }

    public String getFileName(String url) {  
        String filenameWithoutExtension = "";  
        filenameWithoutExtension = String.valueOf(System.currentTimeMillis()  
                + ".jpg");  
        return filenameWithoutExtension;  
    }  


}
Run Code Online (Sandbox Code Playgroud)

当然你可以修改url过滤器,如大写等其他扩展......

在Fragment中,添加以下行:

webPreview.setWebViewClient(new MyWebViewClient(getActivity()));
Run Code Online (Sandbox Code Playgroud)

或者在"活动"中添加以下行:

webPreview.setWebViewClient(new MyWebViewClient(this));
Run Code Online (Sandbox Code Playgroud)

当然,将webPreview修改为您设置的WebView名称

确保添加到WebView设置:

webSettings.setDomStorageEnabled(true);
Run Code Online (Sandbox Code Playgroud)

如果你已经设置:

webSettings.setBlockNetworkImage (false);
Run Code Online (Sandbox Code Playgroud)