一般在项目中与js交互,可能会遇到上传文件图片等操作,避免不了一些坑,下面简单说一下,
public void openFileChooser(ValueCallback uploadMsg)
3.0以上:
public void openFileChooser(ValueCallback uploadMsg, String acceptType)
4.4以下:
public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture)
5.0以上:
public boolean onShowFileChooser(WebView mWebView, ValueCallback filePathCallback, FileChooserParams fileChooserParams)
这里有个漏洞,4.4.x的由于系统内核发生了改变,没法调用以上方法,现在仍然找不到解决办法,唯一的方法就是4.4直接使用手机浏览器打开,这个是可以的。
那下面具体的贴下代码:
继承自WebChromeClient,重写
//3.0++
public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
openFileChooserImpl(uploadMsg);
}
//3.0--
public void openFileChooser(ValueCallback uploadMsg) {
openFileChooserImpl(uploadMsg);
}
//4.1
public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) {
openFileChooserImpl(uploadMsg);
}
@Override
public boolean onShowFileChooser(WebView mWebView, ValueCallback filePathCallback, FileChooserParams fileChooserParams) {
openFileChooserImplForAndroid5(filePathCallback);
return true;
}
外层定义接收返回值:
private ValueCallback mUploadMessage;
private ValueCallback mUploadCallbackAboveL;
不同的版本调不同的方法:
private void openFileChooserImplForAndroid5(ValueCallback uploadMsg) {
mUploadCallbackAboveL = uploadMsg;
dispatchTakePictureIntent();
}
//5.0以下的掉用
private void openFileChooserImpl(ValueCallback uploadMsg) {
mUploadMessage = uploadMsg;
dispatchTakePictureIntent();
}
//拍照
private void dispatchTakePictureIntent() {
selectImgDialog();
}
我下面就把代码全部贴出来吧:
private void takePhoto() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
Uri imageUri = null;
try {
imageUri = Uri.fromFile(createImageFile());
} catch (IOException e) {
e.printStackTrace();
}
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(takePictureIntent, FILECHOOSER_RESULTCODE);
}
}
/*** 209.* 本地相册选择图片* 210.*/ private void chosePic() {
Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT);
String IMAGE_UNSPECIFIED = "image/*";
innerIntent.setType(IMAGE_UNSPECIFIED); // 查看类型
Intent wrapperIntent = Intent.createChooser(innerIntent, null);
startActivityForResult(wrapperIntent, REQ_CHOOSE);
}
String mCurrentPhotoPath = null;
String FileName = "forum";
//创建文件夹包装图片
private File createImageFile() throws IOException {
File storageDir = new File(Util.getAppPath(getActivity()) + FileName);
if (!storageDir.exists()) {
storageDir.mkdirs();
}
storageDir = new File(Util.getAppPath(getActivity()) + FileName + "/", System.currentTimeMillis() + ".jpg");
//保存当前图片路径
mCurrentPhotoPath = storageDir.getAbsolutePath();
return storageDir;
}
//onActivityResult回调
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == FILECHOOSER_RESULTCODE|| requestCode == REQ_CHOOSE) {
if (null == mUploadMessage && null == mUploadCallbackAboveL) return;
Uri result = data == null || resultCode != RESULT_OK? null : data.getData();
if (mUploadCallbackAboveL != null) {
onActivityResultAboveL(requestCode, data);
} else if (mUploadMessage != null) {
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}
}
}
//5.0以上版本,由于api不一样,要单独处理
// @TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void onActivityResultAboveL(int requestCode, Intent data) {
if (mUploadCallbackAboveL == null) {
return;
}
Uri result = null;
if (requestCode == FILECHOOSER_RESULTCODE) {
File file = new File(mCurrentPhotoPath);
Uri localUri = Uri.fromFile(file);
Intent localIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, localUri);
getActivity().sendBroadcast(localIntent);
result = Uri.fromFile(file);
} else if (requestCode == REQ_CHOOSE) {
result = data.getData();
}
mUploadCallbackAboveL.onReceiveValue(new Uri[]{result});
mUploadCallbackAboveL = null;
return;
}
后补的代码:
private void openFileChooserImplForAndroid5(ValueCallback uploadMsg) {
mUploadCallbackAboveL = uploadMsg;
dispatchTakePictureIntent();
}
//5.0以下的掉用
private void openFileChooserImpl(ValueCallback uploadMsg) {
mUploadMessage = uploadMsg;
dispatchTakePictureIntent();
}
基本代码就是以上这些,selectImgDialog()方法是弹窗提示选择,关于这个弹窗布局的用法,之前写的一篇文章说的很清楚了(BottomSheet 的详解及注意事项),有疑问的欢迎提问。