Android webview打开本地图片上传实现代码
作者:ShouCeng 时间:2023-06-02 14:11:50
Webview打开本地图片选择器十分之麻烦,其在安卓系统3x 4x 5x上的行为都不同,处理也不同,所以之前差点崩溃。经过测试和完善,最终其在各个版本上都能完美工作。
直接上代码
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;
}
//扩展浏览器上传文件
//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();
//判断当前历史列表是否最顶端,其实canGoBack已经判断过
if (mWebBackForwardList.getCurrentIndex() > 0) {
webView.goBack();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
标签:android,webview,上传


猜你喜欢
RabbitMQ死信机制实现延迟队列的实战
2023-10-10 18:41:57

Spring中的事务隔离级别和传播行为
2022-07-21 18:18:24

Java ArrayList源码深入分析
2023-06-16 16:30:26

简单总结C++中指针常量与常量指针的区别
2022-06-28 17:33:12
mybatis查询返回Map<String,Object>类型的讲解
2022-12-25 02:07:38

分享一个C#编写简单的聊天程序(详细介绍)
2023-06-19 03:21:59

Android自定义Seekbar滑动条 Pop提示跟随滑动按钮滑动
2021-11-02 20:32:06

Java建造者模式构建复杂对象的最佳实践
2021-06-27 10:07:16

java图形界面之加法计算器
2023-08-31 02:27:43

Android webview与js交换JSON对象数据示例
2022-10-19 18:36:18

使用@Value值注入及配置文件组件扫描
2023-12-01 21:24:12
java原码补码反码关系解析
2021-12-26 20:30:29
C# List<T> Contains<T>()的用法小结
2021-05-29 11:44:56
使用Spring自定义实现IOC和依赖注入(注解方式)
2023-09-16 04:42:35

浅谈Java线程间通信之wait/notify
2022-06-09 11:26:19

Android 偷拍功能实现(手机关闭依然拍照)详解及实例代码
2023-01-08 00:23:19

Android 自定义imageview实现图片缩放实例详解
2023-03-08 10:56:08

C++实现特殊矩阵的压缩存储算法
2023-11-17 23:53:06

android surfaceView实现播放视频功能
2022-12-11 12:55:04
IntelliJ IDEA安装目录和设置目录的说明(IntelliJ IDEA快速入门)
2021-08-16 21:17:08
