android webview在默認(rèn)情況下是不支持網(wǎng)頁中的文件上傳功能的;
如果在網(wǎng)頁中有<input type="file" />,在android webview中訪問時(shí)也會(huì)出現(xiàn)瀏覽文件的按鈕
但是點(diǎn)擊按鈕之后沒有反應(yīng)...
那么如何能夠讓android的webview能夠響應(yīng),這個(gè)瀏覽按鈕呢喉钢?
我們需要為webview設(shè)置WebChromeClient严望,在WebChromeClient的實(shí)現(xiàn)類中覆蓋文件選擇的方法:
private ValueCallback<Uri> mUploadMessage;
/**
* 適配5.0系統(tǒng)
*/
private ValueCallback<Uri[]> mUploadMessage5;
// 上傳事件處理
mWebView.setWebChromeClient(new WebChromeClient() {
// Andorid 4.1+
public void openFileChooser(ValueCallback<Uri> uploadFile,
String acceptType, String capture) {
openFileChooser(uploadFile);
}
// Andorid 3.0 +
public void openFileChooser(ValueCallback<Uri> uploadFile,
String acceptType) {
openFileChooser(uploadFile);
}
// Android 3.0
public void openFileChooser(ValueCallback<Uri> uploadFile) {
mUploadMessage = uploadFile;
OpenUpLoadFolder();
}
// 適配5.0系統(tǒng)
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public boolean onShowFileChooser(WebView webView,
ValueCallback<Uri[]> filePathCallback,
FileChooserParams fileChooserParams) {
mUploadMessage5 = filePathCallback;
OpenUpLoadFolder();
return true;
}
});
/*** 上傳文件對(duì)話框****/
private void OpenUpLoadFolder() {
final CharSequence[] items = { "拍照", "相冊(cè)", "取消" };
AlertDialog dlg = new AlertDialog.Builder(SencondActivity.this)
.setTitle("選擇圖片").setCancelable(false)
.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
// 這里item是根據(jù)選擇的方式,
if (item == 0) {
startActivityForResult(Intent.createChooser(
CameracaptuIntent(), "File Chooser"),
FILECHOOSER_CameraCapture);
} else if (item == 1) {
startActivityForResult(Intent.createChooser(
createCameraIntent(), "File Chooser"),
FILECHOOSER_RESULTCODE);
} else if (item == 2) {
if (mUploadMessage5 != null) {
mUploadMessage5.onReceiveValue(null);
mUploadMessage5 = null;
}
if (mUploadMessage != null) {
mUploadMessage.onReceiveValue(null);
mUploadMessage = null;
}
}
}
}).create();
dlg.show();
}
/*** 選擇圖片文件 ****/
private Intent createCameraIntent() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
//intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
return intent;
}
Uri cameraUri=null;
/*** 拍照 ****/
private Intent CameracaptuIntent() {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 拍照
String sdStatus = Environment.getExternalStorageState();
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 檢測(cè)sd是否可用
// return;
}
File out = new File(
Environment.getExternalStorageDirectory().getPath(), ImageName);
if (!out.exists()) {
out.mkdirs();
}
String Picname = new DateFormat().format("yyyyMMdd_hhmmss",
Calendar.getInstance(Locale.CHINA))
+ ".jpg";
out = new File(Environment.getExternalStorageDirectory().getPath(),
ImageName + "/" + Picname);
cameraUri = Uri.fromFile(out);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, cameraUri);
return cameraIntent;
}
/*** 回調(diào)****/
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == FILECHOOSER_CameraCapture) {
if (null == mUploadMessage && null == mUploadMessage5)
return;
Uri result = data == null || resultCode != RESULT_OK ? null : data.getData();
if (mUploadMessage5 != null) {
Uri[] results = null;
if (resultCode == Activity.RESULT_OK) {
if (data == null) {
results = new Uri[]{cameraUri};
} 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)};
}
} mUploadMessage5.onReceiveValue(results);
mUploadMessage5= null;
} else if (mUploadMessage != null) {
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;}
}
if (requestCode == FILECHOOSER_RESULTCODE) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (null == mUploadMessage5) {
mUploadMessage5.onReceiveValue(null);
mUploadMessage5=null;
return;
}
if (resultCode == RESULT_CANCELED) {
if (mUploadMessage5 != null) {
mUploadMessage5.onReceiveValue(null);
mUploadMessage5=null;
return;
}
}
} else {
if (null == mUploadMessage) {
mUploadMessage.onReceiveValue(null);
mUploadMessage = null;
return;
}
if (resultCode == RESULT_CANCELED) {
if (mUploadMessage != null) {
mUploadMessage.onReceiveValue(null);
mUploadMessage = null;
return;
}
}
}
if (data == null)
return;
Uri result = data == null || resultCode != RESULT_OK ? null : data
.getData();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mUploadMessage5.onReceiveValue(new Uri[] { result });
mUploadMessage5=null;
} else {
mUploadMessage.onReceiveValue(result);
mUploadMessage=null;
}
}
}