android中WebView中H5或者JS选择android本地文件

1,首先自定义WebChromeClient  (这里项目需要只选择图片及文档文案)

package test.demo.bean;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.webkit.PermissionRequest;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

import androidx.annotation.NonNull;

/**
 * @CreateDate: 2021/11/22
 * @Author:lp
 * @Description: 选择本地文件及图片
 */
public class MyWebChromeClient extends WebChromeClient {
    private static final int CHOOSE_REQUEST_CODE = 0x601;
    private Activity mActivity;
    private ValueCallback uploadFile;
    private ValueCallback uploadFiles;

    public static final String DOC = "application/msword";
    public static final String DOCX = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
    public static final String XLS = "application/vnd.ms-excel application/x-excel";
    public static final String XLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    public static final String PPT = "application/vnd.ms-powerpoint";
    public static final String PPTX = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
    public static final String PDF = "application/pdf";
    //选择本地文件类型:图片及文档
    private String[] mimeTypes = {"image/*", DOCX, PPT, PPTX, PDF, XLSX, XLS, DOC};

    public MyWebChromeClient(@NonNull Activity mActivity) {
        this.mActivity = mActivity;
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onPermissionRequest(PermissionRequest request) {
        //需要注意
        request.grant(request.getResources());
    }

    @Override
    public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, FileChooserParams fileChooserParams) {
        this.uploadFiles = filePathCallback;
        openFileChooseProcess();
        return true;
    }

    /**
     * 打开本地存储选择文件
     */
    private void openFileChooseProcess() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        //设置旋转文件类型(如果是所有文件:intent.setType("*/*"))
        intent.setType("image/*" + "|" + DOCX + "|" + PPT + "|" + PPTX + "|" + PDF + "|" + XLSX + "|" + XLS + "|" + DOC);
        intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
        mActivity.startActivityForResult(Intent.createChooser(intent, "Choose"), CHOOSE_REQUEST_CODE);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case CHOOSE_REQUEST_CODE:
                    if (null != uploadFile) {
                        Uri result = data == null || resultCode != Activity.RESULT_OK ? null : data.getData();
                        uploadFile.onReceiveValue(result);
                        uploadFile = null;
                    }
                    if (null != uploadFiles) {
                        Uri result = data == null || resultCode != Activity.RESULT_OK ? null : data.getData();
                        uploadFiles.onReceiveValue(new Uri[]{result});
                        uploadFiles = null;
                    }
                    break;
                default:
                    break;

            }
        } else if (resultCode == Activity.RESULT_CANCELED) {
            if (null != uploadFile) {
                uploadFile.onReceiveValue(null);
                uploadFile = null;
            }
            if (null != uploadFiles) {
                uploadFiles.onReceiveValue(null);
                uploadFiles = null;
            }
        }
    }
}

2,在Activity中对WebView进行设置

  MyWebChromeClient myWebChromeClient = new MyWebChromeClient(this);
  mWebView.setWebChromeClient(myWebChromeClient);

3,在Activity的onActivityResult中调用

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        myWebChromeClient.onActivityResult(requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
    }

你可能感兴趣的:(Android开发,java,H5,android与JS,JS选择手机文件,H5选择本地文件,webView选择本地文件)