WebChromeClient webChromeClient = new WebChromeClient() {
// For Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
if (X5WebViewActivity.this.uploadFile != null) {
X5WebViewActivity.this.uploadFile.onReceiveValue(null);
}
X5WebViewActivity.this.uploadFile = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
startActivityForResult(Intent.createChooser(i, "FileConstant Chooser"), 0);
}
// For Android 3.0+
public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
if (X5WebViewActivity.this.uploadFile != null) {
X5WebViewActivity.this.uploadFile.onReceiveValue(null);
}
X5WebViewActivity.this.uploadFile = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
String type = TextUtils.isEmpty(acceptType) ? "*/*" : acceptType;
i.setType(type);
startActivityForResult(Intent.createChooser(i, "FileConstant Chooser"),
0);
}
// For Android > 4.1.1
@Override
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Log.i("test", "openFileChooser 3");
X5WebViewActivity.this.uploadFile = uploadFile;
selectImage();
}
// For Android >= 5.0
@Override
public boolean onShowFileChooser(com.tencent.smtt.sdk.WebView webView,
ValueCallback<Uri[]> filePathCallback,
WebChromeClient.FileChooserParams fileChooserParams) {
Log.i("test", "openFileChooser 4:" + filePathCallback.toString());
X5WebViewActivity.this.uploadFiles = filePathCallback;
selectImage();
return true;
}
};
protected final void selectImage() {
String[] selectPicTypeStr = {"相机", "文件/图片"};
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setItems(selectPicTypeStr,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
switch (which) {
// 相机拍摄
case 0:
openCarcme();
break;
// 手机相册
case 1:
openFileChooseProcess();
break;
default:
break;
}
}
}).setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
if (uploadFiles != null) {
Uri[] uris = new Uri[1];
uris[0] = Uri.parse("");
uploadFiles.onReceiveValue(uris);
uploadFiles = null;
} else {
uploadFile.onReceiveValue(Uri.parse(""));
uploadFile = null;
}
}
}).show();
}
/**
* 打开照相机
*/
Uri cameraUri;
private void openCarcme() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 必须确保文件夹路径存在,否则拍照后无法完成回调
File vFile = new File(MyAppConstant.FileConstant.DOWNLOAD_FILE_PATH
,System.currentTimeMillis() + ".jpg");
if (!vFile.exists()) {
File vDirPath = vFile.getParentFile();
vDirPath.mkdirs();
} else {
if (vFile.exists()) {
vFile.delete();
}
}
cameraUri = Uri.fromFile(vFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, cameraUri);
startActivityForResult(intent, 1);
}
/**
* 拍照结束后
*/
private void afterOpenCamera() {
File f = new File(MyAppConstant.FileConstant.DOWNLOAD_FILE_PATH
, (System.currentTimeMillis() + ".jpg"));
addImageGallery(f);
}
/**
* 解决拍照后在相册中找不到的问题
*/
private void addImageGallery(File file) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
}
private void openFileChooseProcess() {
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
startActivityForResult(Intent.createChooser(i, "Chooser"), 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case 0:
if (null != uploadFile) {
Uri result = data == null || resultCode != RESULT_OK ? null
: data.getData();
uploadFile.onReceiveValue(result);
uploadFile = null;
}
if (null != uploadFiles) {
Uri result = data == null || resultCode != RESULT_OK ? null
: data.getData();
uploadFiles.onReceiveValue(new Uri[]{result});
uploadFiles = null;
}
break;
case 1:
if (uploadFiles == null) {
return;
}
afterOpenCamera();
Uri[] uris = new Uri[1];
uris[0] = cameraUri;
uploadFiles.onReceiveValue(uris);
break;
default:
break;
}
} else if (resultCode == RESULT_CANCELED) {
if (null != uploadFile) {
uploadFile.onReceiveValue(null);
uploadFile = null;
}
if (null != uploadFiles) {
uploadFiles.onReceiveValue(null);
uploadFiles = null;
}
}
}
Android WebView文件上传(关键代码)
猜你喜欢
转载自blog.csdn.net/u010505059/article/details/81705205
今日推荐
周排行