下载管理器无法在WebView中下载android中的.pdf和.docx文件

时间:2015-05-26 17:33:33

标签: android webview sdk download-manager

我正在使用webview加载此Url, 我的代码加载网址很好但是每当我点击该网址中的文件时它没有下载,我认为问题在于文件扩展名。

我还需要将文件下载到SD卡中的特定文件夹中。

目前iam使用下面的代码我不明白我的代码中可能有什么问题

我的主要活动是

public class GosActs extends Activity{

WebView web;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gos_acts);

    web = (WebView)findViewById(R.id.wGAweb);

    web.setWebViewClient(new WebViewClient(){

        public void onLoadResource(WebView view, String url) {

            //load.setVisibility(View.VISIBLE);
        }

        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            if(url.endsWith(".pdf")){
                Uri source = Uri.parse(url);
                DownloadManager.Request request = new DownloadManager.Request(source);
                request.setDescription("requested GO downloading");
                request.setTitle("go.pdf");

                request.allowScanningByMediaScanner();
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION);
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Go.pdf");

                DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                manager.enqueue(request);
            } else if(url.endsWith(".docx")){
                Uri source = Uri.parse(url);
                DownloadManager.Request request = new DownloadManager.Request(source);
                request.setDescription("requested GO downloading");
                request.setTitle("go.pdf");

                request.allowScanningByMediaScanner();
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION);
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Go.pdf");

                DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                manager.enqueue(request);
            }
            else
            view.loadUrl(url);

            return true;
        }

        public void onPageFinished(WebView view, String url) {
            //load.setVisibility(View.GONE);
        }

    });
    web.getSettings().setJavaScriptEnabled(true);
    web.getSettings().setBuiltInZoomControls(true);
    web.getSettings().setDisplayZoomControls(false);
    web.getSettings().setLoadWithOverviewMode(true);
    web.getSettings().setUseWideViewPort(true);
    web.loadUrl("http://goir.ap.gov.in");


}

我的gos_acts.xml是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#288A0E"
android:orientation="vertical" >


<WebView
    android:id="@+id/wGAweb"
    android:layout_margin="5dip"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background_card" />   

</LinearLayout>

我还在minifest.xml中添加了所需的权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER"/>
<uses-permission android:name="android.permission.INTERNET"/>

0 个答案:

没有答案