安卓webview(h5)调用原生相机及相册返回给h5
时间: 2023-09-04 21:01:51 浏览: 253
安卓的WebView可以通过与原生相机和相册进行交互,从而实现调用相机和相册后将结果返回给H5。
首先,我们需要在AndroidManifest.xml文件中添加相机和相册的权限:
```
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
```
接下来,在我们的WebView的相关代码中,我们需要实现一个WebChromeClient来处理WebView中的文件选择器。在onShowFileChooser方法中,我们可以获取到调用相机和相册所返回的文件路径,然后将其返回给H5页面:
```
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
// Android 5.0+
public boolean onShowFileChooser(WebView view, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
if (mUploadCallbackAboveL != null) {
mUploadCallbackAboveL.onReceiveValue(null);
}
mUploadCallbackAboveL = filePathCallback;
Intent intent = fileChooserParams.createIntent();
try {
startActivityForResult(intent, FILE_CHOOSER_RESULT_CODE);
} catch (ActivityNotFoundException e) {
mUploadCallbackAboveL = null;
return false;
}
return true;
}
// Android 4.1+
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
if (mUploadMessage != null) return;
mUploadMessage = uploadMsg;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
if (capture != null && capture.equals("camera")) {
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 设置相机拍照后保存图片的路径
File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "AppName");
if (!imageStorageDir.exists()) {
imageStorageDir.mkdirs();
}
File file = new File(imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
mCameraPhotoPath = "file:" + file.getAbsolutePath();
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
}
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
try {
startActivityForResult(Intent.createChooser(intent, "选择文件"), FILE_CHOOSER_RESULT_CODE);
} catch (ActivityNotFoundException e) {
mUploadMessage = null;
return;
}
}
});
```
在我们的Activity中,我们需要重写onActivityResult方法,将照片的路径返回给H5页面:
```
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == FILE_CHOOSER_RESULT_CODE) {
if (resultCode == RESULT_OK) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (mUploadCallbackAboveL != null) {
Uri[] results = null;
if (data != null) {
String dataString = data.getDataString();
ClipData clipData = data.getClipData();
if (clipData != null) {
results = new Uri[clipData.getItemCount()];
for (int i = 0; i < clipData.getItemCount(); i++) {
ClipData.Item item = clipData.getItemAt(i);
results[i] = item.getUri();
}
}
if (dataString != null)
results = new Uri[]{Uri.parse(dataString)};
}
mUploadCallbackAboveL.onReceiveValue(results);
mUploadCallbackAboveL = null;
}
} else {
if (mUploadMessage != null) {
Uri result = data == null ? Uri.parse(mCameraPhotoPath) : data.getData();
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}
}
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (mUploadCallbackAboveL != null) {
mUploadCallbackAboveL.onReceiveValue(null);
mUploadCallbackAboveL = null;
}
} else {
if (mUploadMessage != null) {
mUploadMessage.onReceiveValue(null);
mUploadMessage = null;
}
}
}
}
}
```
通过上述代码,当WebView调用相机和相册后,会将结果通过回调的方式返回给H5页面,以便在H5页面上进行处理和展示。
阅读全文