安卓的webView嵌入網頁後,傳送圖片至網頁,網頁請求檔案操作
public class WebViewDemo extends FragmentActivity {
private static final int FILE_SELECT_CODE = 0;
private WebView webView;
private ValueCallback<Uri> mUploadMessage;//回撥圖片選擇,4.4以下
private ValueCallback<Uri[]> mUploadCallbackAboveL;//回撥圖片選擇,5.0以上
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.content_main);
initWebView();
}
@SuppressLint("SetJavaScriptEnabled")
private void initWebView() {
webView = (WebView) findViewById(R.id.webview);
WebView webView = (WebView) findViewById(R.id.webview);
//允許JavaScript執行
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.setVerticalScrollBarEnabled(false);
//執行webview通過URI獲取安卓檔案
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAllowFileAccessFromFileURLs(true);
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
webView.setWebChromeClient(new MyWebChromeClient());//設定可以開啟圖片管理器
webView.loadUrl("file:///android_asset/demo81.htm");
}
private class MyWebChromeClient extends WebChromeClient {
// For Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(Intent.createChooser(i, "File Chooser"), FILE_SELECT_CODE);
}
// For Android 3.0+
public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
startActivityForResult(Intent.createChooser(i, "File Browser"), FILE_SELECT_CODE);
}
// For Android 4.1
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(Intent.createChooser(i, "File Chooser"), FILE_SELECT_CODE);
}
// For Android 5.0+
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
mUploadCallbackAboveL = filePathCallback;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
startActivityForResult(
Intent.createChooser(i, "File Browser"),
FILE_SELECT_CODE);
return true;
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_OK) {
return;
}
switch (requestCode) {
case FILE_SELECT_CODE: {
if (Build.VERSION.SDK_INT >= 21) {//5.0以上版本處理
Uri uri = data.getData();
Uri[] uris = new Uri[]{uri};
/* ClipData clipData = data.getClipData(); //選擇多張
if (clipData != null) {
for (int i = 0; i < clipData.getItemCount(); i++) {
ClipData.Item item = clipData.getItemAt(i);
Uri uri = item.getUri();
uris[i]=uri;
}
}*/
mUploadCallbackAboveL.onReceiveValue(uris);//回撥給js
} else {//4.4以下處理
Uri uri = data.getData();
mUploadMessage.onReceiveValue(uri);
}
}
break;
}
}
}
原文:https://blog.csdn.net/chencaishengsic/article/details/51016679