h5:
H5页面上传手机照片
android
private void webViewClient(WebView webView) {
webView.setWebChromeClient(new WebChromeClient() {
// For Android 3.0+
public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
mUploadFile = uploadMsg;
openImageChooserOperator();
}
// For Android < 3.0
public void openFileChooser(ValueCallback uploadMsgs) {
mUploadFile = uploadMsgs;
openImageChooserOperator();
}
// For Android > 4.1.1
public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) {
mUploadFile = uploadMsg;
openImageChooserOperator();
}
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, FileChooserParams fileChooserParams) {
mUploadFiles = filePathCallback;
openImageChooserOperator();
//mUploadFiles.onReceiveValue(new Uri[]{Uri.parse("content://com.miui.gallery.open/raw/%2Fstorage%2Femulated%2F0%2FDCIM%2FCamera%2FIMG_20200901_074953.jpg")});
//mUploadFiles.onReceiveValue(new Uri[]{Uri.parse("content://media/external/images/media/17714")});
return true;
}
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
Manager.getInstance().getUtils().showLog("message:" + message + "jsResult" + result.toString());
// Toast.makeText(mActivity,"message:" + message,Toast.LENGTH_SHORT).show();
return true;
}
@Override
public void onPermissionRequest(PermissionRequest request) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
request.grant(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE});
}
super.onPermissionRequest(request);
}
});
private void webViewClient(WebView webView) {
webView.setWebChromeClient(new WebChromeClient() {
// For Android 3.0+
public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
mUploadFile = uploadMsg;
openImageChooserOperator();
}
// For Android < 3.0
public void openFileChooser(ValueCallback uploadMsgs) {
mUploadFile = uploadMsgs;
openImageChooserOperator();
}
// For Android > 4.1.1
public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) {
mUploadFile = uploadMsg;
openImageChooserOperator();
}
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, FileChooserParams fileChooserParams) {
mUploadFiles = filePathCallback;
openImageChooserOperator();
return true;
}
});
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == fileUploadRequestCode) {//图片上传
Manager.getInstance().getUtils().showLog(data.getDataString());
if (mUploadFile != null) {
Uri result = data == null ? null : data.getData();
mUploadFile.onReceiveValue(result);
mUploadFile = null;
}
if (mUploadFiles != null) {
Uri result = data == null ? null : data.getData();
mUploadFiles.onReceiveValue(new Uri[]{result});
mUploadFiles = null;
}
}
} else if (resultCode == Activity.RESULT_CANCELED) {
if (requestCode == fileUploadRequestCode) {//图片上传
if (mUploadFile != null) {
mUploadFile.onReceiveValue(null);
mUploadFile = null;
}
if (mUploadFiles != null) {
mUploadFiles.onReceiveValue(null);
mUploadFiles = null;
}
}
}
}