Android WebView调用本地相册的方法
本文实例为大家分享了Android WebView调用本地相册的具体实现方法,供大家参考,具体内容如下
首先要知道android本身的WebView是并不支持调用手机文件并上传的,其次WebView的内核在android每次更新的时候都是不太一样的,也是够坑爹的;不过需求又不能改,H5需要调用系统相册,还好最后还是找到方法解决了,就是要重写里面的一个方法,不过这种情况也是在5.0之前有效,5.0之后就需要重写另外的一个方法,先将这些方法一个个的列出
注意在这里我们需要重写的方法是在这个WebChromeClient类里面的;
private ValueCallback<Uri> mUploadMessage; private ValueCallback<Uri[]> mUploadCallbackAboveL; private final static int FILECHOOSER_RESULTCODE = 101;
当我们是5.0以下的话,用的是以下三个方法:
public void openFileChooser(ValueCallback<Uri> uploadMsg) { Log.d(TAG, "openFileChoose(ValueCallback<Uri> uploadMsg)"); mUploadMessage = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("image/*"); Html5Activity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE); }
public void openFileChooser(ValueCallback uploadMsg, String acceptType) { Log.d(TAG, "openFileChoose( ValueCallback uploadMsg, String acceptType )"); mUploadMessage = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("image/*"); Html5Activity.this.startActivityForResult( Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE); }
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { Log.d(TAG, "openFileChoose(ValueCallback<Uri> uploadMsg, String acceptType, String capture)"); mUploadMessage = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("image/*"); Html5Activity.this.startActivityForResult(Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE); }
值得注意的是这三种方法都是一样的,只是在不同版本下会分别调用,还有就是这个方法是重写这个WebChromeClient类里面的,别以为是我们随便写的,只是google不希望我们重写这个方法罢了,不过5.0之后就又不一样了,需要重写的是下面的这个方法:
@Override public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) { mUploadCallbackAboveL = filePathCallback; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("image/*"); Html5Activity.this.startActivityForResult( Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE); return true; }
好了,到了这一步我们的调用已经ok了,但还有一个需要的过程;跟我们普通的回调接收是一模一样的:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == FILECHOOSER_RESULTCODE) { if (null == mUploadMessage && null == mUploadCallbackAboveL) return; Uri result = data == null || resultCode != RESULT_OK ? null : data.getData(); if (mUploadCallbackAboveL != null) { onActivityResultAboveL(requestCode, resultCode, data); } else if (mUploadMessage != null) { mUploadMessage.onReceiveValue(result); mUploadMessage = null; } } }
当然还有一个是让我们在5.0的情况下用的:
private void onActivityResultAboveL(int requestCode, int resultCode, Intent data) { if (requestCode != FILECHOOSER_RESULTCODE || mUploadCallbackAboveL == null) { return; } Uri[] results = null; if (resultCode == Activity.RESULT_OK) { if (data == null) { } else { String dataString = data.getDataString(); ClipData clipData = data.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(); Log.e(TAG, "onActivityResultAboveL: " + results[i].getPath()); } } if (dataString != null) results = new Uri[]{Uri.parse(dataString)}; Log.e(TAG, "onActivityResultAboveL: " + results.length); } } mUploadCallbackAboveL.onReceiveValue(results); mUploadCallbackAboveL = null; return; }
到这一步H5基本就可以正常的调用手机的相册了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)