webview打开图库并获取安卓文件图片

1,实现安卓webview 打开安卓图库并拿到文件,最后调用js实现文件上传(未实现)。

2,

demo81.htm
网页代码如下:一个简单的文件选择按钮。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
        <input id="fileImage" type="file" size="300" name="fileselect" accept="image/*" />
</body>
</html>

3,安卓实现代码

package cai.com.myapplication;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;


public class WebViewDemo extends FragmentActivity {

    private static final int 
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
安卓WebView组件默认是不支持打开.mht文件的,但可以通过以下方式打开: 1. 通过FileProvider将.mht文件转换为Uri格式,然后传递给WebView进行加载。具体操作如下: ```java // 1. 将.mht文件转换为Uri File mhtFile = new File(Environment.getExternalStorageDirectory(), "test.mht"); Uri uri = FileProvider.getUriForFile(this, "com.example.fileprovider", mhtFile); // 2. 使用WebView加载Uri WebView webView = findViewById(R.id.web_view); webView.getSettings().setAllowFileAccessFromFileURLs(true); webView.getSettings().setAllowUniversalAccessFromFileURLs(true); webView.loadUrl(uri.toString()); ``` 2. 将.mht文件转换为HTML文件,并将HTML文件和相关资源文件一起放入本地文件夹,然后通过WebView加载该文件夹下的HTML文件。具体操作如下: ```java // 1. 将.mht文件转换为HTML文件 File mhtFile = new File(Environment.getExternalStorageDirectory(), "test.mht"); File htmlFile = new File(getFilesDir(), "test.html"); MhtUtil.convertMhtToHtml(mhtFile.getAbsolutePath(), htmlFile.getAbsolutePath()); // 2. 将HTML文件和相关资源文件放入本地文件夹 File htmlFolder = new File(getFilesDir(), "html"); if (!htmlFolder.exists()) { htmlFolder.mkdir(); } FileUtils.copyFile(htmlFile, new File(htmlFolder, htmlFile.getName())); FileUtils.copyFolder(new File(mhtFile.getParent()), htmlFolder); // 3. 使用WebView加载本地文件夹下的HTML文件 WebView webView = findViewById(R.id.web_view); webView.getSettings().setAllowFileAccessFromFileURLs(true); webView.getSettings().setAllowUniversalAccessFromFileURLs(true); webView.loadUrl("file://" + htmlFolder.getAbsolutePath() + "/" + htmlFile.getName()); ``` 其中,MhtUtil是一个自定义的工具类,用于将.mht文件转换为HTML文件。FileUtils是一个自定义的工具类,用于文件文件夹的复制操作。需要注意的是,复制文件文件夹的操作需要在子线程中执行,以免阻塞主线程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值