android - DownloadListener 不下载图片?

标签 android download android-webview listener

在我的 Android 应用程序中,我使用带有 downloadListener 的 WebView。我想访问一个 Intranet 站点,当单击一个链接时,使用 mimetype 信息决定如何处理该文件。

问题在于引用图像文件(png、jpg 等)的链接。它们不会触发监听器,而是自动在新页面中显示文件,跳过监听器。

我怎样才能改变这种行为?

我的代码没什么特别的...

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

    webview = (WebView)findViewById(R.id.webview);
    webview.setWebViewClient(new MiWebClient());
    webview.setDownloadListener(oyenteDescarga);

    //...other onCreate stuff... nothing relevant
}

DownloadListener oyenteDescarga = new DownloadListener()
{
    @Override
    public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) 
    {
      //...I want to use mimetype here, but this listener is not triggered when I click an image, an example:
      Toast.makeText(getBaseContext(), "URL:" + url, Toast.LENGTH_SHORT).show(); //it doesn't show when i click a link referencing an image
    }
};

MiWebClient 是扩展WebViewClient 的类

public class MiWebClient extends WebViewClient
{
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) 
    {
        view.loadUrl(url);
        return true;
    }
}

最佳答案

基于documentation :

Registers the interface to be used when content can not be handled by the rendering engine, and should be downloaded instead. This will replace the current handler.

由于图像可以由渲染引擎处理,它不会下载它,因此不会调用下载管理器。

关于android - DownloadListener 不下载图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15746077/

相关文章:

java - 让 linearLayout 从屏幕上消失?

php - 如何在 Android Studio 中将所有 textview 和 edittext 字段插入到数据库 mysql 的同一列中

java - 从 Java 调用 Javascript

android - 我可以在 Android 应用程序中使用字形图标吗?

linux - 在Linux中定期从FTP下载文件

c# - Webclient 启动慢

android - 隐藏WebView中的滚动条

android - 使用一段时间后,我的应用程序在滚动 WebView 时卡住,显示 "could not lock surface"

android - 如何覆盖android webview os 4.1+的默认文本选择?

java - iPhone Java 字符串代码到对象 C