第一步
要设置webView 支持js setJavaScriptEnabled
第二步要设置 WebView
setWebChromeClient 的事件 此事件有 几个方法
先声明一个各个版本对应的参数
private ValueCallback<Uri> mUM;
//For Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
mUM = uploadMsg;
}
// For Android 3.0+, above method not supported in some android 3+ versions, in such case we use this
public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
mUM = uploadMsg;
}
//For Android 4.1+
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
mUM = uploadMsg;
}
//For Android 5.0+
public boolean onShowFileChooser(
WebView webView, ValueCallback<Uri[]> filePathCallback,
FileChooserParams fileChooserParams) {
mUM = uploadMsg;
}
这几个方法都是对应sdk的版本 根据版本会执行各自的方法
接下来就是在对应的方法中 写对应的逻辑
肯定是要授权权限 SD卡的读写权限 还有摄像头的权限 这些权限要在AndroidManifest 注册的
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.CAMERA" />
但是要针对6.0以后要动态申请权限
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.CAMERA,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
//获取存储权限
public static boolean getStoragePermissions(Activity activity) {
int permission = ActivityCompat.checkSelfPermission(activity,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
int read = ActivityCompat.checkSelfPermission(activity,
Manifest.permission.READ_EXTERNAL_STORAGE);
int camera = ActivityCompat.checkSelfPermission(activity,
Manifest.permission.CAMERA);
if (permission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE);
return false;
}
if (camera != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE);
return false;
}
if (read != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE);
return false;
}
return true;
}
先判断有没有权限 再去申请权限 在申请权限的时候 需要注意 webView再点击之后要给他一个反应 不然接下来你可能会怀疑人生千万别揪头发 那样秃的更快 [哈哈]
if (mUMA != null) {
Uri[] results = new Uri[]{Uri.EMPTY};
mUMA.onReceiveValue(results);
mUMA = null;
}
申请权限之前必须给他一个通知
拍照因为10.0的特性 拍照跟9.0以前还不一样
想判断到底当前到底是什么版本
/** * 获取是不是Android 10 * * @param * @return * @throws IOException */
public static boolean getAndroidQ() { return Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q; }
/**
* 创建图片地址uri,用于保存拍照后的照片 Android 10以后使用这种方法
*/
public static Uri createImageUri(Activity activity) {
String status = Environment.getExternalStorageState();
// 判断是否有SD卡,优先使用SD卡存储,当没有SD卡时使用手机存储
if (status.equals(Environment.MEDIA_MOUNTED)) {
return activity.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());
} else {
return activity.getContentResolver().insert(MediaStore.Images.Media.INTERNAL_CONTENT_URI, new ContentValues());
}
}
if (Utils.getAndroidQ()) {
photoUri = Utils.createImageUri(activity);
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
activity.startActivityForResult(takePictureIntent, FCR);
} else {
try {
photoFile = Utils.createImageFile(activity);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
photoUri = Uri.fromFile(photoFile);
} else {
photoUri = FileProvider.getUriForFile(activity, activity.getPackageName() + ".fileProvider", photoFile);
}
mCM = "file:" + photoFile.getAbsolutePath();
filePath = photoFile.getAbsolutePath();
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
activity.startActivityForResult(takePictureIntent, FCR);
} catch (IOException e) {
e.printStackTrace();
}
}
接下来就是接受选择的图片了 那肯定是在 OnActivityResult中喽
if (resultCode == Activity.RESULT_OK) {
if (requestCode == FCR) {
if (null == mUMA) {
return;
}
if (intent == null) {
if (Utils.getAndroidQ() && isPhoto) {
results = new Uri[]{photoUri};
} else {
if (mCM != null) {
results = new Uri[]{afterChosePic(filePath, compressPath)};
}
}
} else {
if (Utils.getAndroidQ() && isPhoto) {
results = new Uri[]{photoUri};
} else {
//9.0 8.0 7.0 6.0
if (isPhoto) {//拍照
results = new Uri[]{photoUri};
} else {
String dataString = intent.getDataString();
if (dataString != null) {
results = new Uri[]{Uri.parse(dataString)};
}
}
}
}
}
}
mUMA.onReceiveValue(results);
mUMA = null;
到这里就差不多就完了 我在可能在使用的时候可能代码有点乱没有封装 可能时间比较紧 你们如果有时间也可能封装下会好点
贴下完整的代码
public class UpFileWebChromeClient extends WebChromeClient {
Activity activity;
private String mCM;
private String filePath = "";
Uri photoUri = null;
private ValueCallback<Uri> mUM;
private ValueCallback<Uri[]> mUMA;
private final static int FCR = 1;
String compressPath = "";
public CallBackInterface titleChangeCallBack;
public void setTitleChangeCallBack(CallBackInterface titleChangeCallBack) {
this.titleChangeCallBack = titleChangeCallBack;
}
public UpFileWebChromeClient(Activity activity) {
this.activity = activity;
}
//For Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
selectImage();
mUM = uploadMsg;
// showPrictore();
}
// For Android 3.0+, above method not supported in some android 3+ versions, in such case we use this
public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
if (WBH5FaceVerifySDK.getInstance().recordVideoForApiBelow21(uploadMsg, acceptType, activity)) {
if (!PermissionMamager.getStoragePermissions(activity)) {
if (mUMA != null) {
Uri[] results = new Uri[]{Uri.EMPTY};
mUMA.onReceiveValue(results);
mUMA = null;
}
if (mUM != null) {
mUM.onReceiveValue(Uri.EMPTY);
mUM = null;
}
return;
}
return;
}
selectImage();
mUM = uploadMsg;
showPrictore();
}
//For Android 4.1+
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
if (WBH5FaceVerifySDK.getInstance().recordVideoForApiBelow21(uploadMsg, acceptType, activity)) {
if (!PermissionMamager.getStoragePermissions(activity)) {
if (mUMA != null) {
Uri[] results = new Uri[]{Uri.EMPTY};
mUMA.onReceiveValue(results);
mUMA = null;
}
if (mUM != null) {
mUM.onReceiveValue(Uri.EMPTY);
mUM = null;
}
return;
}
return;
}
selectImage();
mUM = uploadMsg;
showPrictore();
}
@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
if (titleChangeCallBack != null) {
titleChangeCallBack.onFinish(title);
}
}
//For Android 5.0+
public boolean onShowFileChooser(
WebView webView, ValueCallback<Uri[]> filePathCallback,
FileChooserParams fileChooserParams) {
if (WBH5FaceVerifySDK.getInstance().recordVideoForApi21(webView, filePathCallback, activity, fileChooserParams)) {
if (!PermissionMamager.getStoragePermissions(activity)) {
if (mUMA != null) {
Uri[] results = new Uri[]{Uri.EMPTY};
mUMA.onReceiveValue(results);
mUMA = null;
}
if (mUM != null) {
mUM.onReceiveValue(Uri.EMPTY);
mUM = null;
}
return true;
}
return true;
}
selectImage();
mUMA = filePathCallback;
showPrictore();
return true;
}
boolean isPhoto = false;
private void showPrictore() {
List<ButtonListBean> buttonListDaos = new ArrayList<>();
for (int i = 0; i < 2; i++) {
ButtonListBean buttonListDao = new ButtonListBean();
buttonListDao.setType(i);
if (i == 0) {
buttonListDao.setContent(activity.getResources().getString(R.string.photo));
} else {
buttonListDao.setContent(activity.getResources().getString(R.string.select_photo));
}
buttonListDaos.add(buttonListDao);
}
PopWindowBList popWindowBList = new PopWindowBList(activity, buttonListDaos, activity.getResources().getString(R.string.please_select), activity.getResources().getString(R.string.cancel));
popWindowBList.setOnTouchOutside(false);
popWindowBList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
File photoFile = null;
ButtonListBean buttonListDao = (ButtonListBean) adapter.getItem(position);
assert buttonListDao != null;
if (buttonListDao.getType() == 0) {//相机
if (!PermissionMamager.getStoragePermissions(activity)) {
if (mUMA != null) {
Uri[] results = new Uri[]{Uri.EMPTY};
mUMA.onReceiveValue(results);
mUMA = null;
}
if (mUM != null) {
mUM.onReceiveValue(Uri.EMPTY);
mUM = null;
}
return;
}
isPhoto = true;
if (Utils.getAndroidQ()) {
photoUri = Utils.createImageUri(activity);
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
activity.startActivityForResult(takePictureIntent, FCR);
} else {
try {
photoFile = Utils.createImageFile(activity);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
photoUri = Uri.fromFile(photoFile);
} else {
photoUri = FileProvider.getUriForFile(activity, activity.getPackageName() + ".fileProvider", photoFile);
}
mCM = "file:" + photoFile.getAbsolutePath();
filePath = photoFile.getAbsolutePath();
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
activity.startActivityForResult(takePictureIntent, FCR);
} catch (IOException e) {
e.printStackTrace();
}
}
} else {//相册
if (!PermissionMamager.getStorageSDPermissions(activity)) {
if (mUMA != null) {
Uri[] results = new Uri[]{Uri.EMPTY};
mUMA.onReceiveValue(results);
mUMA = null;
}
if (mUM != null) {
mUM.onReceiveValue(Uri.EMPTY);
mUM = null;
}
return;
}
isPhoto = false;
Intent intent = new Intent();
intent.setAction(Intent.ACTION_PICK);
intent.setType("image/*");
activity.startActivityForResult(intent, FCR);
}
}
});
popWindowBList.setDismiss(this::cancelCallback);
popWindowBList.show();
}
/**
* 打开图库,同时处理图片
*/
private void selectImage() {
compressPath = Environment.getExternalStorageDirectory().getPath() + "/QWB/temp";
File file = new File(compressPath);
if (!file.exists()) {
file.mkdirs();
}
compressPath = compressPath + File.separator + "compress.png";
File image = new File(compressPath);
if (image.exists()) {
image.delete();
}
}
/**
* 选择照片后结束
*/
private Uri afterChosePic(String oldPath, String newPath) {
File newFile;
try {
newFile = FileUtils.compressFile(oldPath, newPath);
} catch (Exception e) {
e.printStackTrace();
newFile = null;
}
return Uri.fromFile(newFile);
}
/**
* 防止点击dialog的取消按钮之后,就不再次响应点击事件了
*/
public void cancelCallback() {
if (mUMA != null) {
Uri[] results = new Uri[]{Uri.EMPTY};
mUMA.onReceiveValue(results);
mUMA = null;
}
if (mUM != null) {
mUM.onReceiveValue(Uri.EMPTY);
mUM = null;
}
}
//处理上传图片回来于图片压缩
public void setOnActivityResult(int requestCode, int resultCode, Intent intent) {
if (mUMA == null) return;
if (Build.VERSION.SDK_INT >= 21) {
Uri[] results = null;
if (resultCode == Activity.RESULT_OK) {
if (requestCode == FCR) {
if (null == mUMA) {
return;
}
if (intent == null) {
if (Utils.getAndroidQ() && isPhoto) {
results = new Uri[]{photoUri};
} else {
if (mCM != null) {
results = new Uri[]{afterChosePic(filePath, compressPath)};
}
}
} else {
if (Utils.getAndroidQ() && isPhoto) {
results = new Uri[]{photoUri};
} else {
//9.0 8.0 7.0 6.0
if (isPhoto) {//拍照
results = new Uri[]{photoUri};
} else {
String dataString = intent.getDataString();
if (dataString != null) {
results = new Uri[]{Uri.parse(dataString)};
}
}
}
}
}
}
mUMA.onReceiveValue(results);
mUMA = null;
} else {
if (requestCode == FCR) {
if (null == mUM) return;
Uri result = intent == null || resultCode != Activity.RESULT_OK ? null : intent.getData();
mUM.onReceiveValue(result);
mUM = null;
}
}
}
在头秃的路上越来越远...
好了 打完收工 白白 [旺财]