0

我添加了一个 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 文件。

任何帮助都会很棒谢谢

4

1 回答 1

1

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

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

DownloadListener 示例:如何使用 DownloadListener?

于 2013-02-18T20:43:02.810 回答