【问题标题】:How to force a file to download in-app instead of showing up in the default browser如何强制文件在应用内下载而不是显示在默认浏览器中
【发布时间】:2014-04-24 01:06:37
【问题描述】:

我使用 WebView 创建了一个 Android 应用程序,但我面临的唯一问题是文件在默认浏览器中打开而不是下载。

这是 WebActivity 代码:

public class WebActivity extends Activity {
private WebView webView;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    webView = (WebView) findViewById(R.id.webView01);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(new myWebClient()
    {
    @Override
    public void onPageFinished(WebView view, String url) {

        findViewById(R.id.imageLoading1).setVisibility(View.GONE);

        findViewById(R.id.webView01).setVisibility(View.VISIBLE);
    }


});     
    webView.loadUrl("domain");
    webView.setDownloadListener(new DownloadListener() {
        public void onDownloadStart(String url, String userAgent,
                String contentDisposition, String mimetype,
                long contentLength) {
          Intent i = new Intent(Intent.ACTION_VIEW);
          i.setData(Uri.parse(url));
          startActivity(i);
        }
    }); 

}


public class myWebClient extends WebViewClient
{
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        view.loadUrl(url);
        return true;
    }
}

 @Override
  public boolean onKeyDown(int keyCode, KeyEvent event)
  {
   if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
     webView.goBack();
     return true;
 }
 return super.onKeyDown(keyCode, event);
  }
  }

【问题讨论】:

    标签: android webview webclient


    【解决方案1】:

    浏览器应用程序正在打开,因为它是处理 Intents 的适当应用程序,将 URL 作为其数据,这就是您在 DownloadListener 中使用的内容。

    如果您不希望浏览器启动,请使用来自DownloadListener 的 URL 打开一个InputStream,然后自己从文件中获取字节。

    public void saveUrl(final String filename, final String urlString)
        throws MalformedURLException, IOException {
       BufferedInputStream in = null;
       FileOutputStream fout = null;
       try {
           in = new BufferedInputStream(new URL(urlString).openStream());
           fout = new FileOutputStream(filename);
    
           final byte data[] = new byte[1024];
           int count;
           while ((count = in.read(data, 0, 1024)) != -1) {
              fout.write(data, 0, count);
           }
       } finally {
           if (in != null) {
               in.close();
           }
           if (fout != null) {
               fout.close();
           }
       }
    }
    

    【讨论】:

    • 感谢您的回复 - 我尝试了 Inputstream 但再次在新浏览器中打开它,请您提供代码
    • @user3566830 我添加了将文件从 URL 保存到文件中的代码。
    • 感谢代码,但它仍然不起作用我在 webView.setDownloadListener(new DownloadListener() { 和 public void onDownloadStart(String url, String userAgent) 之前添加了代码更具体的在哪里添加代码
    • @user3566830 从onDownloadStart 调用saveUrl 并删除Intent 的东西。
    • 它在我删除 Intent 并从 onDownloadStart 调用 saveUrl 的代码中给出了很多错误,你能提供一个源代码吗?你的时间很多
    猜你喜欢
    • 2016-07-31
    • 2015-01-25
    • 2014-07-16
    • 1970-01-01
    • 2013-10-15
    • 1970-01-01
    • 2011-09-11
    相关资源
    最近更新 更多