WebView如何调用系统的相机和相册上传文件

大家可以通过产看WebViewClient这个类里面的源码可知,在下面的几个方法里面5.0以下是通过ValueCallbackuploadMsg 来向webview上传我们通过拍照或相册里面获取到的uri而在5.0以上是通过ValueCallbackfilePathCallback 来上传,大家注意这里变成了一个Uri类型的数组,然后我们只需要在我们的openFileChoose()和onShowFileChooser()里面写我们调用系统相机和相册的代码了然后在onActivityResult里面获取Uri,

我们首先要分别声明ValueCallback和ValueCallBack在openFileChoose和onShowFileChooser里面进行赋值就像我下面代码一样

private ValueCallback mUploadMessage;// 表单的数据信息
private ValueCallback mUploadCallbackAboveL;
private final static int FILECHOOSER_RESULTCODE = 1;// 表单的结果回调
private Uri imageUri;

/**
* web视图
*/
mWebView.setWebChromeClient(new WebChromeClient() {
//设置网页加载的进度条
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
if (newProgress == 100) {
mProgressBar.setVisibility(View.GONE);
} else {
mProgressBar.setVisibility(View.VISIBLE);
mProgressBar.setProgress(newProgress);
}
}
//android>5.0调用这个方法
@Override
public boolean onShowFileChooser(WebView webView,
ValueCallback filePathCallback,
FileChooserParams fileChooserParams) {
mUploadCallbackAboveL=filePathCallback;
take();
return true;
}
public void openFileChooser(ValueCallback uploadMsg) {
mUploadMessage=uploadMsg;
take();
}
public void openFileChooser(ValueCallback uploadMsg,String acceptType) {
mUploadMessage=uploadMsg;
take();
}
public void openFileChooser(ValueCallback uploadMsg,String acceptType, String capture) {
mUploadMessage=uploadMsg;
take();
}
});
if (!TextUtils.isEmpty(mWebUrl)) {
mBrowserLayout.loadUrl(mWebUrl);
} else {
Toast.makeText(this, "获取URL地址失败", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==FILECHOOSER_RESULTCODE)
{
if (null == mUploadMessage && null == mUploadCallbackAboveL) return;
Uri result = data == null || resultCode != RESULT_OK ? null : data.getData();
if (mUploadCallbackAboveL != null) {
onActivityResultAboveL(requestCode, resultCode, data);
}
else if (mUploadMessage != null) {
Log.e("result",result+"");
if(result==null){
// mUploadMessage.onReceiveValue(imageUri);
mUploadMessage.onReceiveValue(imageUri);
mUploadMessage = null;

Log.e("imageUri",imageUri+"");
}else {
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}


}
}
}

@SuppressWarnings("null")
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void onActivityResultAboveL(int requestCode, int resultCode, Intent data) {
if (requestCode != FILECHOOSER_RESULTCODE
|| mUploadCallbackAboveL == null) {
return;
}

Uri[] results = null;
if (resultCode == Activity.RESULT_OK) {
if (data == null) {
results = new Uri[]{imageUri};
} else {
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)};
}
}
if(results!=null){
mUploadCallbackAboveL.onReceiveValue(results);
mUploadCallbackAboveL = null;
}else{
results = new Uri[]{imageUri};
mUploadCallbackAboveL.onReceiveValue(results);
mUploadCallbackAboveL = null;
}

return;
}

private void take(){
File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyApp");
// Create the storage directory if it does not exist
if (! imageStorageDir.exists()){
imageStorageDir.mkdirs();
}
File file = new File(imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
imageUri = Uri.fromFile(file);

final List cameraIntents = new ArrayList();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List 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, imageUri);
cameraIntents.add(i);

}
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
Intent chooserIntent = Intent.createChooser(i,"Image Chooser");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
ServiceWebActivity.this.startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
}

你可能感兴趣的:(WebView如何调用系统的相机和相册上传文件)