今天就跟大家聊聊有關(guān)Android中怎么利用WebView上傳文件,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

1.接收WebView打開文件選擇器的通知
2.收到通知后,打開文件選擇器等待用戶選擇需要上傳的文件
3.在onActivityResult中得到用戶選擇的文件的Uri
4.然后把Uri傳遞給Html5
這樣就完成了一次H5選擇文件的過程,下面我把代碼貼出來自習(xí)看一下
首先,WebView必須要支持JS交互,所以要打開JS交互
mWebView.getSettings().setJavaScriptEnabled(true);
當(dāng)H5在調(diào)用上傳文件的Api的時(shí)候,WebView會(huì)回調(diào) openFileChooser和onShowFileChooser 方法來通知我們,我們這個(gè)時(shí)候要做的就是重寫這個(gè)方法
需要注意的是這個(gè)方法在不同的Api上會(huì)回調(diào)不同行參方法
mWebView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress == 100) {
mBar.setVisibility(View.GONE);
} else {
mBar.setVisibility(View.VISIBLE);
mBar.setProgress(newProgress);
}
super.onProgressChanged(view, newProgress);
}
//For Android API < 11 (3.0 OS)
public void openFileChooser(ValueCallback<Uri> valueCallback) {
uploadMessage = valueCallback;
openImageChooserActivity();
}
//For Android API >= 11 (3.0 OS)
public void openFileChooser(ValueCallback<Uri> valueCallback, String acceptType, String capture) {
uploadMessage = valueCallback;
openImageChooserActivity();
}
//For Android API >= 21 (5.0 OS)
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
uploadMessageAboveL = filePathCallback;
openImageChooserActivity();
return true;
}
});我們?cè)趏penFileChooser方法中先保存了一下ValueCallback的回調(diào)對(duì)象,這個(gè)對(duì)象最后用來通知H5文件地址,我們之后在調(diào)用openFileChooser方法來打開文件選擇器
private void openImageChooserActivity() {
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(Intent.createChooser(i, "Image Chooser"), FILE_CHOOSER_RESULT_CODE);
}當(dāng)用戶選擇完文件后,會(huì)調(diào)用onActivityResult方法,我們重寫并等待回調(diào)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == FILE_CHOOSER_RESULT_CODE) {
if (null == uploadMessage && null == uploadMessageAboveL) return;
Uri result = data == null || resultCode != RESULT_OK ? null : data.getData();
if (uploadMessageAboveL != null) {
onActivityResultAboveL(requestCode, resultCode, data);
} else if (uploadMessage != null) {
uploadMessage.onReceiveValue(result);
uploadMessage = null;
}
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void onActivityResultAboveL(int requestCode, int resultCode, Intent intent) {
if (requestCode != FILE_CHOOSER_RESULT_CODE || uploadMessageAboveL == null)
return;
Uri[] results = null;
if (resultCode == Activity.RESULT_OK) {
if (intent != null) {
String dataString = intent.getDataString();
ClipData clipData = intent.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)};
}
}
uploadMessageAboveL.onReceiveValue(results);
uploadMessageAboveL = null;
}看完上述內(nèi)容,你們對(duì)Android中怎么利用WebView上傳文件有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
分享名稱:Android中怎么利用WebView上傳文件-創(chuàng)新互聯(lián)
網(wǎng)站網(wǎng)址:http://www.chinadenli.net/article32/dpcdpc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、網(wǎng)站改版、網(wǎng)站排名、響應(yīng)式網(wǎng)站、App開發(fā)、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容