<uses-permission android:name="android.hardware.camera"/>
// 申请权限
private final int GET_PERMISSION_REQUEST = 100; //权限申请自定义码
private void getPermissions() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager
.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager
.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager
.PERMISSION_GRANTED) {
takePhoto();
} else {
//不具有获取权限,需要进行权限申请
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.RECORD_AUDIO,
Manifest.permission.CAMERA}, GET_PERMISSION_REQUEST);
}
} else {
takePhoto();
}
}
// js和原生交互的入口
webView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
// (1)该方法回调时说明版本API >= 21,此时将结果赋值给 mUploadCallbackAboveL,使之 != null
mUploadCallbackAboveL = filePathCallback;
// takePhoto();
getPermissions();
return true;
}
});
// 跳转到原生的拍照界面
private void takePhoto() {
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(Intent.createChooser(i, "Image Chooser"), 100);
}
//处理file返回的结果
File fileUri = new File(Environment.getExternalStorageDirectory().getPath() + "/" + SystemClock.currentThreadTimeMillis() + ".jpg");
Uri imageUri = Uri.fromFile(fileUri);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 100) {
//针对5.0以上, 以下区分处理方法
if (mUploadCallbackBelow != null) {
chooseBelow(resultCode, data);
} else if (mUploadCallbackAboveL != null) {
chooseAbove(resultCode, data);
} else {
Toast.makeText(this, "发生错误", Toast.LENGTH_SHORT).show();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
/**
* Android API >= 21(Android 5.0) 版本的回调处理
*
* @param resultCode 选取文件或拍照的返回码
* @param data 选取文件或拍照的返回结果
*/
private void chooseAbove(int resultCode, Intent data) {
if (RESULT_OK == resultCode) {
updatePhotos();
if (data != null) {
// 这里是针对从文件中选图片的处理, 区别是一个返回的URI, 一个是URI[]
Uri[] results;
Uri uriData = data.getData();
if (uriData != null) {
results = new Uri[]{uriData};
mUploadCallbackAboveL.onReceiveValue(results);
} else {
mUploadCallbackAboveL.onReceiveValue(null);
}
} else {
mUploadCallbackAboveL.onReceiveValue(new Uri[]{imageUri});
}
} else {
mUploadCallbackAboveL.onReceiveValue(null);
}
mUploadCallbackAboveL = null;
}
private void updatePhotos() {
// 该广播即使多发(即选取照片成功时也发送)也没有关系,只是唤醒系统刷新媒体文件
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(imageUri);
sendBroadcast(intent);
}
/**
* Android API < 21(Android 5.0)版本的回调处理
*
* @param resultCode 选取文件或拍照的返回码
* @param data 选取文件或拍照的返回结果
*/
private void chooseBelow(int resultCode, Intent data) {
if (RESULT_OK == resultCode) {
updatePhotos();
if (data != null) {
// 这里是针对文件路径处理
Uri uri = data.getData();
if (uri != null) {
mUploadCallbackBelow.onReceiveValue(uri);
} else {
mUploadCallbackBelow.onReceiveValue(null);
}
} else {
// 以指定图像存储路径的方式调起相机,成功后返回data为空
mUploadCallbackBelow.onReceiveValue(imageUri);
}
} else {
mUploadCallbackBelow.onReceiveValue(null);
}
mUploadCallbackBelow = null;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == GET_PERMISSION_REQUEST) {
int size = 0;
if (grantResults.length >= 1) {
int writeResult = grantResults[0];
//读写内存权限
boolean writeGranted = writeResult == PackageManager.PERMISSION_GRANTED;//读写内存权限
if (!writeGranted) {
size++;
}
//录音权限
int recordPermissionResult = grantResults[1];
boolean recordPermissionGranted = recordPermissionResult == PackageManager.PERMISSION_GRANTED;
if (!recordPermissionGranted) {
size++;
}
//相机权限
int cameraPermissionResult = grantResults[2];
boolean cameraPermissionGranted = cameraPermissionResult == PackageManager.PERMISSION_GRANTED;
if (!cameraPermissionGranted) {
size++;
}
if (size == 0) {
// startCamera();
takePhoto();
} else {
Toast.makeText(this, "请到设置-权限管理中开启", Toast.LENGTH_SHORT).show();
}
}
}
}