【问题标题】:How to use a download Manager in a webview to download pdf/ppt/doc files to sdcard如何在 webview 中使用下载管理器将 pdf/ppt/doc 文件下载到 sdcard
【发布时间】:2014-01-14 04:40:10
【问题描述】:

我正在开发一个 android 应用程序,它使用 webview 打开我的网站(url:learnportal.me),然后,在输入正确的凭据后,它将加载一个普通的 html 页面,该页面有多个下载链接(那些都是 pdf/ppt/doc 文件)。
所有这些都发生在 webview 中。

我想要的只是直接在应用程序本身内进行下载,而不需要重定向到移动设备中的默认网络浏览器。

此外,我需要将文件下载到 SD 卡。

这是我目前正在处理的 MainActivity 页面……但不确定它是否正确

public class MainActivity extends Activity {  
 @SuppressWarnings("deprecation")  
 @SuppressLint("SetJavaScriptEnabled")  
 @Override  
 protected void onCreate(Bundle savedInstanceState) {  
      super.onCreate(savedInstanceState);  
      WebView webview = new WebView(this);  
      webview.setWebChromeClient(new WebChromeClient());  
      WebViewClient client = new ChildBrowserClient();  
      webview.setWebViewClient(client);  
      WebSettings settings = webview.getSettings();  
      settings.setJavaScriptEnabled(true);  
      //webview.setInitialScale(1);  
      //webview.getSettings().setUseWideViewPort(true);  
      settings.setJavaScriptCanOpenWindowsAutomatically(false);  
      //settings.setBuiltInZoomControls(true);  
      settings.setPluginState(PluginState.ON);  
      settings.setDomStorageEnabled(true);  
      webview.loadUrl("http://learnportal.me");  
      //webview.setId(5);  
      //webview.setInitialScale(0);  
      //webview.requestFocus();  
     // webview.requestFocusFromTouch();  
      setContentView(webview);  
 }  
 /**  
  * The webview client receives notifications about appView  
  */  
 public class ChildBrowserClient extends WebViewClient {  
      @SuppressLint({ "InlinedApi", "NewApi" })  
      @Override  
      public boolean shouldOverrideUrlLoading(WebView view, String url) {  
           boolean value = true;  
           String extension = MimeTypeMap.getFileExtensionFromUrl(url);  
           if (extension != null) {  
                MimeTypeMap mime = MimeTypeMap.getSingleton();  
                String mimeType = mime.getMimeTypeFromExtension(extension);  
                if (mimeType != null) {  
                     if (mimeType.toLowerCase().contains("pptx")  
                               || extension.toLowerCase().contains("pdf")  
                               || extension.toLowerCase().contains("doc")
                               || extension.toLowerCase().contains("ppt")){  
                          DownloadManager mdDownloadManager = (DownloadManager) MainActivity.this  
                                    .getSystemService(Context.DOWNLOAD_SERVICE);  
                          DownloadManager.Request request = new DownloadManager.Request(  
                                    Uri.parse(url));
                          File destinationFile = new File(Environment.getExternalStorageDirectory(),getFileName(url));
                          request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
                          request.setDescription("Downloading via LearnPortal..");  
                          request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                          request.setDestinationUri(Uri.fromFile(destinationFile));
                          mdDownloadManager.enqueue(request);  
                          value = false;  
                     }  
                }  
                if (value) {  
                     view.loadUrl(url);  
                }  
           }  
           return value;  
      }  
      @Override  
      public void onPageFinished(WebView view, String url) {  
           super.onPageFinished(view, url);  
      }  
      /**  
       * Notify the host application that a page has started loading.  
       *   
       * @param view  
       *      The webview initiating the callback.  
       * @param url  
       *      The url of the page.  
       */  
      @Override  
      public void onPageStarted(WebView view, String url, Bitmap favicon) {  
           super.onPageStarted(view, url, favicon);  
      }  
 }  
 /**  
  * File name from URL  
  *   
  * @param url  
  * @return  
  */  
 public String getFileName(String url) {  
     String filenameWithoutExtension = "";  
     filenameWithoutExtension = String.valueOf(System.currentTimeMillis());  
     return filenameWithoutExtension;  
}
}

【问题讨论】:

    标签: android webview


    【解决方案1】:

    【讨论】:

    • 当然我会试一试。如果这里有人可以根据我的要求修复我的上述代码,那将是一个很大的帮助..我已经为此工作了一个星期,试图找出正确的方法解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-18
    • 2016-04-02
    • 2016-01-06
    • 2013-08-27
    • 2014-05-13
    相关资源
    最近更新 更多