记录在Android的webview加载的h5中调用相机和选择相册的步骤
1申请权限
此时需要申请相机和相册的权限
(1)android.Manifest.permission.CAMERA)
(2)android.Manifest.permission.READ_EXTERNAL_STORAGE
private void getPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(getActivity(), android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(getActivity(), android.Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED ) {
Fragment.this.requestPermissions(new String[]{android.Manifest.permission.CAMERA, android.Manifest.permission.READ_EXTERNAL_STORAGE}, 103);
} else {
gotoWebActivity();
}
} else {
gotoWebActivity();
}
}
在申请结束的回调中进行判断
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 103:
if (grantResults.length > 0) {
List deniedPermissions = new ArrayList<>();
for (int i = 0; i < grantResults.length; i++) {
int grantResult = grantResults[i];
String permission = permissions[i];
if (grantResult != PackageManager.PERMISSION_GRANTED) {
deniedPermissions.add(permission);
}
}
LogCat.d("========", deniedPermissions);
//被拒绝权限
if (deniedPermissions.isEmpty()) {
gotoWebActivity();
} else {
ToastUtil.makeText(getActivity(), "请前往权限管理开启相机和相册相关权限", Toast.LENGTH_SHORT).show();
}
}
break;
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
因为我是在Fragment中申请的权限,所以我在用的 fragment.this.requestPermissions();如果是在Activity中申请的话用ActivityCompat
2继承WebChromeClient
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
这样WebSettings的设置就不说了,这里主要继承WebChromeClient这个类重写一些方法
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.NonNull;
import android.view.View;
import android.webkit.PermissionRequest;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.ProgressBar;
import android.widget.TextView;
public class PaxWebChromeClient extends WebChromeClient {
private static final int CHOOSE_REQUEST_CODE = 0x9001;
private Activity mActivity;
private ValueCallback uploadFile;//定义接受返回值
private ValueCallback uploadFiles;
private ProgressBar bar;
private TextView mTitle;
public PaxWebChromeClient(@NonNull Activity mActivity, ProgressBar bar, TextView title) {
this.mActivity = mActivity;
this.bar=bar;
this.mTitle=title;
}
@Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress == 100) {
bar.setVisibility(View.INVISIBLE);
} else {
if (View.INVISIBLE == bar.getVisibility()) {
bar.setVisibility(View.VISIBLE);
}
bar.setProgress(newProgress);
}
super.onProgressChanged(view, newProgress);
}
@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
mTitle.setText(title);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onPermissionRequest(PermissionRequest request) {
// super.onPermissionRequest(request);//必须要注视掉
request.grant(request.getResources());
}
// For Android 3.0+
public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
this.uploadFile = uploadFile;
openFileChooseProcess();
}
// For Android < 3.0
public void openFileChooser(ValueCallback uploadMsgs) {
this.uploadFile = uploadFile;
openFileChooseProcess();
}
// For Android > 4.1.1
// @Override
public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) {
this.uploadFile = uploadFile;
openFileChooseProcess();
}
// For Android >= 5.0
@Override
public boolean onShowFileChooser(WebView webView,
ValueCallback filePathCallback,
WebChromeClient.FileChooserParams fileChooserParams) {
this.uploadFiles = filePathCallback;
openFileChooseProcess();
return true;
}
private void openFileChooseProcess() {
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
mActivity.startActivityForResult(Intent.createChooser(i, "Choose"), CHOOSE_REQUEST_CODE);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
LogCat.d("requestCode===",requestCode+"====");
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case CHOOSE_REQUEST_CODE:
if (null != uploadFile) {
Uri result = data == null || resultCode != Activity.RESULT_OK ? null
: data.getData();
uploadFile.onReceiveValue(result);
uploadFile = null;
}
if (null != uploadFiles) {
Uri result = data == null || resultCode != Activity.RESULT_OK ? null
: data.getData();
uploadFiles.onReceiveValue(new Uri[]{result});
uploadFiles = null;
}
break;
default:
break;
}
} else if (resultCode == Activity.RESULT_CANCELED) {
if (null != uploadFile) {
uploadFile.onReceiveValue(null);
uploadFile = null;
}
if (null != uploadFiles) {
uploadFiles.onReceiveValue(null);
uploadFiles = null;
}
}
}
}
ProgressBar bar, TextView title这两个是我为加载H5页面时设置进度条和设置标题是用的,如不需要可以删除,在onProgressChanged这个方法里设置进度条,在onReceivedTitle这个方法里拿到H5页面的标题。
重写onPermissionRequest这个方法用来获取权限
在不同的系统调用h5的方法是不一样的。这个代码中有明确的表示
3加载WebChromeClient
private PaxWebChromeClient chromeClient;
private void initView() {
chromeClient = new PaxWebChromeClient(this,bar,mTitle);
webView.setWebChromeClient(chromeClient);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
chromeClient.onActivityResult(requestCode,resultCode,data);
super.onActivityResult(requestCode, resultCode, data);
}
实例并加载PaxWebChromeClient ,并在activity或者fragment中的onActivityResult中回调PaxWebChromeClient的onActivityResult方法。