一、引言
Android webview是不會默認實現(xiàn)web里的<input type="file" />的選擇文件功能,要想實現(xiàn)web的選擇文件功能需要webview做一定的處理。
二、功能實現(xiàn)
重寫 WebChromeClient中的 openFileChooser() 和 onShowFileChooser()方法,使用原生代碼來調(diào)用本地相冊、拍照或文件的功能,最后在 onActivityResult 把選擇的文件URI 回傳回去。
1、重寫 WebChromeClient中的 openFileChooser() 和 onShowFileChooser()方法,由于在不同安卓版本的方法不大一樣,所以要分別進行實現(xiàn)。
mWebView.setWebChromeClient(new WebChromeClient(){
// For Android < 3.0
public void openFileChooser(ValueCallback<Uri> valueCallback) {
mUploadCallBack = valueCallback;
showFileChooser();
}
// For Android >= 3.0
public void openFileChooser(ValueCallback valueCallback, String acceptType) {
mUploadCallBack = valueCallback;
showFileChooser();
}
//For Android >= 4.1
public void openFileChooser(ValueCallback<Uri> valueCallback, String acceptType, String capture) {
mUploadCallBack = valueCallback;
showFileChooser();
}
// For Android >= 5.0
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
mUploadCallBackAboveL = filePathCallback;
showFileChooser();
return true;
}
});
showFileChooser()方法代碼如下:
/**
* 打開選擇文件/相機
*/
private void showFileChooser() {
// Intent intent1 = new Intent(Intent.ACTION_PICK, null);
// intent1.setDataAndType(
// MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
Intent intent1 = new Intent(Intent.ACTION_GET_CONTENT);
intent1.addCategory(Intent.CATEGORY_OPENABLE);
intent1.setType("*/*");
Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mCameraFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator +
System.currentTimeMillis() + ".jpg";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// android7.0注意uri的獲取方式改變
Uri photoOutputUri = FileProvider.getUriForFile(
MainActivity.this,
BuildConfig.APPLICATION_ID + ".fileProvider",
new File(mCameraFilePath));
intent2.putExtra(MediaStore.EXTRA_OUTPUT, photoOutputUri);
} else {
intent2.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(mCameraFilePath)));
}
Intent chooser = new Intent(Intent.ACTION_CHOOSER);
chooser.putExtra(Intent.EXTRA_TITLE, "File Chooser");
chooser.putExtra(Intent.EXTRA_INTENT, intent1);
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{intent2});
startActivityForResult(chooser, REQUEST_CODE_FILE_CHOOSER);
}
2、在onActivityResult里處理獲取到的文件。
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_FILE_CHOOSER) {
Uri result = data == null || resultCode != RESULT_OK ? null : data.getData();
if (result == null && !TextUtils.isEmpty(mCameraFilePath)) {
// 看是否從相機返回
File cameraFile = new File(mCameraFilePath);
if (cameraFile.exists()) {
result = Uri.fromFile(cameraFile);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, result));
}
}
if (result != null) {
String path = FileUtils.getPath(this, result);
if (!TextUtils.isEmpty(path)) {
File f = new File(path);
if (f.exists() && f.isFile()) {
Uri newUri = Uri.fromFile(f);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (mUploadCallBackAboveL != null) {
if (newUri != null) {
mUploadCallBackAboveL.onReceiveValue(new Uri[]{newUri});
mUploadCallBackAboveL = null;
return;
}
}
} else if (mUploadCallBack != null) {
if (newUri != null) {
mUploadCallBack.onReceiveValue(newUri);
mUploadCallBack = null;
return;
}
}
}
}
}
clearUploadMessage();
return;
}
}
/**
* webview沒有選擇文件也要傳null,防止下次無法執(zhí)行
*/
private void clearUploadMessage() {
if (mUploadCallBackAboveL != null) {
mUploadCallBackAboveL.onReceiveValue(null);
mUploadCallBackAboveL = null;
}
if (mUploadCallBack != null) {
mUploadCallBack.onReceiveValue(null);
mUploadCallBack = null;
}
}
注意1:不管有沒有選擇文件,都要執(zhí)行onReceiveValue,沒有選擇文件的話就傳null。
注意2:代碼里并沒有直接把data.getData()得到的uri傳給onReceiveValue方法,因為有些非圖片類型(比如pdf)的uri傳回去的話h5也不能正確拿到,所以這邊先通過uri獲取到文件路徑,再轉(zhuǎn)化為uri傳回去。所以上面代碼先通過result得到path再轉(zhuǎn)化為newUri并不是多此一舉的。
通過uri得到path方法參考這篇文章:http://m.itdecent.cn/p/25c35da68db2
注意3:如果你的應(yīng)用混淆了要注意下,openFileChooser方法并不是WebChromeClient的對外開放的方法,因此這個方法會被混淆,解決辦法也比較簡單,只需要在混淆文件里控制一下即可:
-keepclassmembers class * extends android.webkit.WebChromeClient{
public void openFileChooser(...);
}
如果<input type="file" />只是調(diào)用系統(tǒng)相冊/相機并進行圖片壓縮功能,不用考慮其他文件類型的話可以參考http://m.itdecent.cn/p/444932cf5d41。
好了,如果對你有幫助的話點個贊、關(guān)注我吧。