Android webview打開本地圖片上傳實(shí)現(xiàn)代碼
Webview打開本地圖片選擇器十分之麻煩,其在安卓系統(tǒng)3x 4x 5x上的行為都不同,處理也不同,所以之前差點(diǎn)崩潰。經(jīng)過測(cè)試和完善,最終其在各個(gè)版本上都能完美工作。
直接上代碼
package com.testandroid.webview;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.ValueCallback;
import android.webkit.WebBackForwardList;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import com.testandroid.R;
public class WebViewActivity extends AppCompatActivity {
private final String TAG = WebViewActivity.class.getSimpleName();
private Button button;
private WebView webView;
private String recgPic = "http://m.shitu.chinaso.com/mx/index.html";
public final static int FILECHOOSER_RESULTCODE = 1;
public final static int FILECHOOSER_RESULTCODE_FOR_ANDROID_5 = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
initTestWebView();
}
private void initTestWebView() {
webView = (WebView) findViewById(R.id.tempWebView);
WiewSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
builder.setTitle("xxx提示").setMessage(message).setPositiveButton("確定", null);
builder.setCancelable(false);
builder.setIcon(R.mipmap.ic_launcher);
AlertDialog dialog = builder.create();
dialog.show();
result.confirm();
return true;
}
//擴(kuò)展瀏覽器上傳文件
//3.0++版本
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
openFileChooserImpl(uploadMsg);
}
//3.0--版本
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
openFileChooserImpl(uploadMsg);
}
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
openFileChooserImpl(uploadMsg);
}
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
onenFileChooseImpleForAndroid(filePathCallback);
return true;
}
});
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.loadUrl(recgPic);
}
public ValueCallback<Uri> mUploadMessage;
private void openFileChooserImpl(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"), FILECHOOSER_RESULTCODE);
}
public ValueCallback<Uri[]> mUploadMessageForAndroid5;
private void onenFileChooseImpleForAndroid(ValueCallback<Uri[]> filePathCallback) {
mUploadMessageForAndroid5 = filePathCallback;
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
contentSelectionIntent.setType("image/*");
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE_FOR_ANDROID_5);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,Intent intent) {
if (requestCode == FILECHOOSER_RESULTCODE) {
if (null == mUploadMessage)
return;
Uri result = intent == null || resultCode != RESULT_OK ? null: intent.getData();
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
} else if (requestCode == FILECHOOSER_RESULTCODE_FOR_ANDROID_5){
if (null == mUploadMessageForAndroid5)
return;
Uri result = (intent == null || resultCode != RESULT_OK) ? null: intent.getData();
if (result != null) {
mUploadMessageForAndroid5.onReceiveValue(new Uri[]{result});
} else {
mUploadMessageForAndroid5.onReceiveValue(new Uri[]{});
}
mUploadMessageForAndroid5 = null;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (webView.canGoBack() && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
//獲取歷史列表
WebBackForwardList mWebBackForwardList = webView
.copyBackForwardList();
//判斷當(dāng)前歷史列表是否最頂端,其實(shí)canGoBack已經(jīng)判斷過
if (mWebBackForwardList.getCurrentIndex() > 0) {
webView.goBack();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android中使用achartengine生成圖表的具體方法
這篇文章主要介紹了Android中使用achartengine生成圖表的具體方法,有需要的朋友可以參考一下2014-01-01
Android實(shí)現(xiàn)TV端大圖瀏覽效果的全過程
最近的開發(fā)中遇到了個(gè)需求,需要在tv端加載很長的圖片,發(fā)現(xiàn)網(wǎng)上沒有相關(guān)的資料,所以跟大家分享下,這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)TV端大圖瀏覽效果的相關(guān)資料,需要的朋友可以參考下2023-01-01
21天學(xué)習(xí)android開發(fā)教程之SurfaceView與多線程的混搭
21天學(xué)習(xí)android開發(fā)教程之SurfaceView與多線程的混搭,感興趣的小伙伴們可以參考一下2016-02-02
Android輕松實(shí)現(xiàn)多語言的方法示例
本篇文章主要介紹了Android輕松實(shí)現(xiàn)多語言的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11
Android多線程+單線程+斷點(diǎn)續(xù)傳+進(jìn)度條顯示下載功能
這篇文章主要介紹了Android多線程+單線程+斷點(diǎn)續(xù)傳+進(jìn)度條顯示下載功能,需要的朋友可以參考下2017-06-06
Android pull解析xml的實(shí)現(xiàn)方法
這篇文章主要介紹了Android pull解析xml的實(shí)現(xiàn)方法的相關(guān)資料,希望通過本文能幫助到大家,謝謝大家對(duì)本站的支持!需要的朋友可以參考下2017-10-10
Android開發(fā)控制ScrollView滑動(dòng)速度的方法
這篇文章主要介紹了Android開發(fā)控制ScrollView滑動(dòng)速度的方法,結(jié)合實(shí)例形式分析了Android編程中ScrollView滑動(dòng)事件相關(guān)操作技巧,需要的朋友可以參考下2017-02-02
安卓(Android)開發(fā)之統(tǒng)計(jì)App啟動(dòng)時(shí)間
當(dāng)大家要改善APP啟動(dòng)速度優(yōu)化的時(shí)候,首先要知道App的啟動(dòng)時(shí)間,那么改如何統(tǒng)計(jì)時(shí)間呢,下面我們一起來看看。2016-08-08

