Android WebView设置下载文件夹位置

时间:2013-01-08 18:57:16

标签: android webview download directory

我添加了一个webview活动,想要控制我的最终用户通过我的应用程序下载的任何文件夹。这些文件将是pdf文件,我希望它们进入我的应用程序使用的特定文件夹,而不是默认的下载文件夹。

public class myweb extends Activity {

 private WebView myWebView;
 private String LOG_TAG = "AndroidWebViewActivity";

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


        myWebView = (WebView) findViewById(R.id.webView1);

        //enable Javascript
        myWebView.getSettings().setJavaScriptEnabled(true);

        //loads the WebView completely zoomed out
        myWebView.getSettings().setLoadWithOverviewMode(true);

        //true makes the Webview have a normal viewport such as a normal desktop browser
        //when false the webview will have a viewport constrained to it's own dimensions
        myWebView.getSettings().setUseWideViewPort(true);

        //override the web client to open all links in the same webview
        myWebView.setWebViewClient(new MyWebViewClient());
        myWebView.setWebChromeClient(new MyWebChromeClient());

        //Injects the supplied Java object into this WebView. The object is injected into the
        //JavaScript context of the main frame, using the supplied name. This allows the
        //Java object's public methods to be accessed from JavaScript.
        myWebView.addJavascriptInterface(new JavaScriptInterface(this), "Android");

        //load the home page URL
        //myWebView.loadUrl("http://demo.mysamplecode.com/Servlets_JSP/pages/androidWebView.jsp");
        myWebView.loadUrl("http://www.patonsyarns.com/pattern.php?PID=4521&cps=21191");


        myWebView.setDownloadListener(new DownloadListener() {
            public void onDownloadStart(String url, String userAgent,
                    String contentDisposition, String mimetype, long contentLength) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                startActivity(intent);

            }
        });

}



 private class MyWebViewClient extends WebViewClient {
     @Override
     public boolean shouldOverrideUrlLoading(WebView view, String url) {
         if (Uri.parse(url).getHost().equals("demo.mysamplecode.com")) {
             return false;
         }
         Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
         startActivity(intent);
         return true;
     }
 }


 private class MyWebChromeClient extends WebChromeClient {

      //display alert message in Web View
      @Override
         public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
             Log.d(LOG_TAG, message);
             new AlertDialog.Builder(view.getContext())
              .setMessage(message).setCancelable(true).show();
             result.confirm();
             return true;
         }

     }

     public class JavaScriptInterface {
         Context mContext;

         // Instantiate the interface and set the context
         JavaScriptInterface(Context c) {
             mContext = c;
         }

         //using Javascript to call the finish activity
         public void closeMyActivity() {
             finish();
         }

     }

     //Web view has record of all pages visited so you can go back and forth
     //just override the back button to go back in history if there is page
     //available for display
     @Override
     public boolean onKeyDown(int keyCode, KeyEvent event) {
         if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
             myWebView.goBack();
             return true;
         }
         return super.onKeyDown(keyCode, event);
     }

我事先并不知道用户将下载哪些文件,而不是pdf文件。

任何帮助都会很棒 感谢

1 个答案:

答案 0 :(得分:1)

我很担心唯一的解决方案是使用DownloadListener来捕获事件并手动下载文件。

下载文件示例:http://www.androidhive.info/2012/04/android-downloading-file-by-showing-progress-bar/

DownloadListener示例:How do I use DownloadListener?