android - 以全屏模式从 WebView 打开图像

标签 android android-webview

如果webview html内容很少<img>标签,当用户在 web View 中点击图像时,是否可以在新 Activity 中以全屏模式打开该图像?

最佳答案

是的。基本上可以从 WebView 中调用任何类型的 native Android 代码。您定义要公开的方法,以便从类中的 WebView 中进行调用,将此类的实例传递给 WebView,从那时起,这些方法就可以调用了就像 JavaScript 方法一样。这是示例(这是从我的另一个答案复制粘贴的;您需要将视频 Intent 替换为图像显示 Intent ):

从 Web View 中调用 native 代码:
创建 Web View 时添加 javascript 接口(interface)(基本上是 java 类,其方法将公开以通过 Web View 中的 javascript 调用)。

JavaScriptInterface jsInterface = new JavaScriptInterface(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(jsInterface, "JSInterface");

javascript接口(interface)类本身的定义(这是我从我的另一个答案中获取的示例类,并以 native Intent 打开视频)

public class JavaScriptInterface {
    private Activity activity;

    public JavaScriptInterface(Activity activiy) {
        this.activity = activiy;
    }

    public void startVideo(String videoAddress){
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.parse(videoAddress), "video/3gpp"); // The Mime type can actually be determined from the file
        activity.startActivity(intent);
    }
}

现在,如果您想从页面的 html 中调用此代码,请提供以下方法:

<script>
  function playVideo(video){
    window.JSInterface.startVideo(video);
  }
</script>

因此,您需要向 JSInterface 添加适当的方法,并从 Web 应用程序中调用代码。

关于android - 以全屏模式从 WebView 打开图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9719012/

相关文章:

java - 如何在 Android 中使用 Volley 手动设置自定义 cookie 和 header 值?

java - 是否可以在 Java 中声明一个 1 位变量?

android - HttpURLConnection (java.net.CookieManager) 和 WebView (android.webkit.CookieManager) 之间 cookie 的两种同步方式

android - 从 AccessibilityService 在 Chrome 中执行 Javascript

android - 当页面不可用错误时调用 WebView onPageFinished。解决方案?

android - 默认 Android EditText 高亮颜色不是橙色

java - 如何保持数据库中的数据处于 Activity 状态

java - 通过回车键提交方法?

webview - 如何在 Xamarin.Forms 中的 Android WebView 上启用本地存储

android - YouTube仅在WebView中播放音频