Android-WebView上传图片实现(5.0及以上相关问题
2019-08-14 本文已影响3人
MonkeyLei
最近小萌新需要实现“发布页面的加载以及处理成功、失败、添加图片等js回调”。基本的Js调用android本地方法,大家尝试尝试应该就了解个大概了。这块也没怎么深入,后面再深入看看吧!
随便看几个图就晓得了赛:
image一般都是定义一个类来弄,感觉清爽点:
/**
* js调用android方法
*/
class JsOperationLMPA {
Activity mActivity;
public JsOperationLMPA(Activity activity) {
mActivity = activity;
}
/**
* 发布失败,带参数
*
* @param _message
*/
@JavascriptInterface
public void uploadGoodsFailer(String _message) {
showToast(_message);
}
/**
* 发布成功,带参数
*
* @param _id
*/
@JavascriptInterface
public void uploadGoodsSuccess(final int _id) {
}
}
然后初始化下Webview相关设置:- Mixed Content问题要注意,视频播放不了可能就是这个问题!
/**
* 初始化Webview
*/
private void initWebView(WebView webView) {
///< 开启JavaScript支持
webView.getSettings().setJavaScriptEnabled(true);
///< 加入JS接口
webView.addJavascriptInterface(new JsOperationLMPA(this), "uploadGoods");
// 设置WebView是否支持使用屏幕控件或手势进行缩放,默认是true,支持缩放
webView.getSettings().setSupportZoom(true);
///< 设置WebView是否使用其内置的变焦机制,该机制结合屏幕缩放控件使用,默认是false,不使用内置变焦机制。
webView.getSettings().setBuiltInZoomControls(true);
///< 设置是否开启DOM存储API权限,默认false,未开启,设置为true,WebView能够使用DOM storage API
webView.getSettings().setDomStorageEnabled(true);
///< 触摸焦点起作用.如果不设置,则在点击网页文本输入框时,不能弹出软键盘及不响应其他的一些事件。
webView.requestFocus();
///< 设置此属性,可任意比例缩放,设置webview推荐使用的窗口
webView.getSettings().setUseWideViewPort(false);
///< 设置webview加载的页面的模式,缩放至屏幕的大小
webView.getSettings().setLoadWithOverviewMode(true);
///< 允许webview对文件的操作
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAllowFileAccessFromFileURLs(true);
///< 设置进度
webView.setWebChromeClient(new LineMinePublishActivity.MyWebChromClient());
///< 设置加载状态
webView.setWebViewClient(new LineMinePublishActivity.MyWebClient());
// android 5.0以上默认不支持Mixed Content
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
}
至于其他相关问题,查查就好了!回调应该就ojbk了。
最后再处理下图片上传的方法的重写就好了,感谢 https://blog.csdn.net/fuzhongbin/article/details/78769527, 为了防止丢失以及方便使用,还是转载下网友的代码:
/**
* 自定义WebClient
* 增加本地相册调用处理
*/
private ValueCallback<Uri> mUploadMessage;
private ValueCallback<Uri[]> uploadMessageAboveL;
private static final int FILE_CHOOSER_RESULT_CODE = 12;
private class MyWebChromClient extends WebChromeClient {
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
}
// For Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
mUploadMessage = uploadMsg;
openImageChooserActivity();
}
// For Android 3.0+
public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
mUploadMessage = uploadMsg;
openImageChooserActivity();
}
// For Android 4.1
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
mUploadMessage = uploadMsg;
openImageChooserActivity();
}
// For Android >= 5.0
@Override
public boolean onShowFileChooser(WebView webView,
ValueCallback<Uri[]> filePathCallback,
WebChromeClient.FileChooserParams fileChooserParams) {
uploadMessageAboveL = filePathCallback;
openImageChooserActivity();
return true;
}
}
/**
* 打开本地相册
*/
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);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == FILE_CHOOSER_RESULT_CODE) {
if (null == mUploadMessage && null == uploadMessageAboveL) return;
Uri result = data == null || resultCode != RESULT_OK ? null : data.getData();
if (uploadMessageAboveL != null) {
onActivityResultAboveL(requestCode, resultCode, data);
} else if (mUploadMessage != null) {
mUploadMessage.onReceiveValue(result);
mUploadMessage = 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;
}