[图片上传中。。。(1)]http://blog.csdn.net/android_it/article/details/52538995 http://blog.csdn.net/android_it/article/details/52538995 Android WebView 不支持 H5 input type="file" 解决方法
Android webview JS交互,调取本地相册,并且实现上传。
首先我们要理解整个互动过程,在网页webview的的组件是 input输入框,输入类型为file,点击-----> 调用 安卓 不同的版本
private static final int FILE_SELECT_CODE = 0;
private ValueCallback<Uri> mUploadMessage;
private ValueCallback<Uri[]> umUploadMessages;
private WebChromeClient.FileChooserParams mfileChooserParams;
private int FILECHOOSER_RESULTCODE_5 = 100;
private int FILECHOOSER_RESULTCODE = 1000;
mWebView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
AlertDialog.Builder b2 = new AlertDialog.Builder(mContext)
.setTitle("友情提示:").setMessage(message)
.setPositiveButton("ok",
new AlertDialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
result.confirm();
// MyWebView.this.finish();
}
});
b2.setCancelable(false);
b2.create();
b2.show();
return true;
}
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
umUploadMessages = filePathCallback;
mfileChooserParams = fileChooserParams;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.putExtra("return-data", true);
i.setType("image/*");
i.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
"image/*");
startActivityForResult(Intent.createChooser(i, "选择相册"),
FILECHOOSER_RESULTCODE_5);
return true;
}
// 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.putExtra("return-data", true);
i.setType("image/*");
i.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
"image/*");
startActivityForResult(Intent.createChooser(i, "选择相册"),
FILECHOOSER_RESULTCODE);
}
// 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.putExtra("return-data", true);
i.setType("image/*");
i.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
"image/*");
startActivityForResult(Intent.createChooser(i, "选择相册"),
FILECHOOSER_RESULTCODE);
}
// Andorid 3.0 +
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.putExtra("return-data", true);
i.setType("image/*");
i.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
"image/*");
startActivityForResult(Intent.createChooser(i, "选择相册"),
FILECHOOSER_RESULTCODE);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == FILECHOOSER_RESULTCODE) {
if (null == mUploadMessage) return;
if (null == data) {
mUploadMessage.onReceiveValue(null);
mUploadMessage = null;
} else {
Uri result = data == null || resultCode != RESULT_OK ? null
: data.getData();
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}
} else if (requestCode == FILECHOOSER_RESULTCODE_5) {// 5.0适配,主要是因为5.0的返回参数不同。
// 处理5.0的callback
if (umUploadMessages != null) {
if (null != data) {
// 5.0的处理方式不同,要注意。
ArrayList<String> resultList = data
.getStringArrayListExtra("data");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
umUploadMessages.onReceiveValue(
mfileChooserParams.parseResult(resultCode, data));
}
umUploadMessages = null;
} else {
umUploadMessages.onReceiveValue(null);
}
}
}
}
参考文章:
http://blog.csdn.net/android_it/article/details/52538995
private WebView wv;
private String url ="http://shanghai.job1s.com/wap/member/index.php?c=photo";
private ValueCallback<Uri> mUploadMessage;
public ValueCallback<Uri[]> uploadMessage;
public static final int REQUEST_SELECT_FILE = 100;
private final static int FILECHOOSER_RESULTCODE = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wv = (WebView) findViewById(R.id.webview);
//设置支持Javascript
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl(url);
wv.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器
view.loadUrl(url);
return false;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
}
@Override
public void onPageFinished(WebView view, String url) {
}
});
wv.setWebChromeClient(new WebChromeClient(){
// For 3.0+ Devices (Start)
// onActivityResult attached before constructor
protected void openFileChooser(ValueCallback uploadMsg, String acceptType)
{
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE);
}
// For Lollipop 5.0+ Devices
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)
{
if (uploadMessage != null) {
uploadMessage.onReceiveValue(null);
uploadMessage = null;
}
uploadMessage = filePathCallback;
Intent intent = fileChooserParams.createIntent();
try
{
startActivityForResult(intent, REQUEST_SELECT_FILE);
} catch (ActivityNotFoundException e)
{
uploadMessage = null;
Toast.makeText(getBaseContext(), "Cannot Open File Chooser", Toast.LENGTH_LONG).show();
return false;
}
return true;
}
//For Android 4.1 only
protected void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture)
{
mUploadMessage = uploadMsg;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "File Browser"), FILECHOOSER_RESULTCODE);
}
protected 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"), FILECHOOSER_RESULTCODE);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
if (requestCode == REQUEST_SELECT_FILE)
{
if (uploadMessage == null)
return;
uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));
uploadMessage = null;
}
}
else if (requestCode == FILECHOOSER_RESULTCODE)
{
if (null == mUploadMessage)
return;
// Use MainActivity.RESULT_OK if you're implementing WebView inside Fragment
// Use RESULT_OK only if you're implementing WebView inside an Activity
Uri result = intent == null || resultCode != MainActivity.RESULT_OK ? null : intent.getData();
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}
else
Toast.makeText(getBaseContext(), "Failed to Upload Image", Toast.LENGTH_LONG).show();
}
android webview使用html5<input id="input" type="file"/> 上传相册、拍照照片