2020年7月21日更新
适配安卓10方式:
返回URI即可
概述
手机上加载webview,网页上上传图片调用原生相机和相册上传图片
安卓手机上适配也很简单调用webview的setWebChromeClient方法即可。
流程
先设置好webview的加载以及websetting,这里就不多说了。
setWebChromeClient
变量参数
//5.0以下使用
private ValueCallback<Uri> uploadMessage;
// 5.0及以上使用
private ValueCallback<Uri[]> uploadMessageAboveL;
//拍照图片路径
private String cameraFielPath;
private final static int FILE_CAMERA_RESULT_CODE = 129;
主要是setWebChromeClient方法的实现
mWebView.setWebChromeClient(new WebChromeClient() {
// For Android < 3.0
public void openFileChooser(ValueCallback<Uri> valueCallback) {
uploadMessage = valueCallback;
take();
}
// For Android >= 3.0
public void openFileChooser(ValueCallback valueCallback, String acceptType) {
uploadMessage = valueCallback;
take();
}
//For Android >= 4.1
public void openFileChooser(ValueCallback<Uri> valueCallback, String acceptType, String capture) {
uploadMessage = valueCallback;
take();
}
// For Android >= 5.0
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
uploadMessageAboveL = filePathCallback;
take();
return true;
}
});
这个方法分几个版本的适配3.0以下的设备,3.0到4.1的设备,4.1到5.0的设备,以及5.0以上的设备,每个方法参数不一样适配时请注意。5.0以下的参数都是 ValueCallback<Uri> ,以上的设备需要 ValueCallback<Uri[]> ,回传数据的时候也是对应的不要弄错了。
意图处理
private void take() {
//拍照图片保存位置
File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyApp");
if ( !imageStorageDir.exists() ) {
imageStorageDir.mkdirs();
}
cameraFielPath = imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg";
File file = new File(cameraFielPath);
//需要显示应用的意图列表,这个list的顺序和选择菜单上的图标顺序是相关的,请注意。
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
//获取手机里所有注册相机接收意图的应用程序,放到意图列表里(无他相机,美颜相机等第三方相机)
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for (ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent i = new Intent(captureIntent);
i.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
i.setPackage(packageName);
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
cameraIntents.add(i);
}
//相册选择器
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
//intent选择器
Intent chooserIntent = Intent.createChooser(i, "选择模式");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
this.startActivityForResult(chooserIntent, FILE_CAMERA_RESULT_CODE);
}
onActivityResult回调
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if ( null == uploadMessage && null == uploadMessageAboveL ) {
return;
}
//没有返回值时的处理
if ( resultCode != RESULT_OK ) {
//需要回调onReceiveValue方法防止下次无法响应js方法
if ( uploadMessageAboveL != null ) {
uploadMessageAboveL.onReceiveValue(null);
uploadMessageAboveL = null;
}
if ( uploadMessage != null ) {
uploadMessage.onReceiveValue(null);
uploadMessage = null;
}
return;
}
Uri result = null;
if ( requestCode == FILE_CAMERA_RESULT_CODE ) {
if ( null != data && null != data.getData() ) {
result = data.getData();
}
if ( result == null && hasFile(cameraFielPath) ) {
result = Uri.fromFile(new File(cameraFielPath));
}
//5.0以上设备的数据处理
if ( uploadMessageAboveL != null ) {
uploadMessageAboveL.onReceiveValue(new Uri[]{result});
uploadMessageAboveL = null;
} else if ( uploadMessage != null ) {
//5.0以下设备的数据处理
uploadMessage.onReceiveValue(result);
uploadMessage = null;
}
}
}
最后的效果如下: