我有一个带有输入文件的Web视图,并且我使用此代码打开相机来拾取图像:
// ...
private ValueCallback<Uri[]> mUMA;
// ...
webView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, android.os.Message resultMsg) {
// TARGET BLANK OPENED IN BROWSER
WebView newWebView = new WebView(view.getContext());
newWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);
return true;
}
});
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(newWebView);
resultMsg.sendToTarget();
return true;
}
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
}
//For Android 5.0+
public boolean onShowFileChooser(
WebView webView, ValueCallback<Uri[]> filePathCallback,
WebChromeClient.FileChooserParams fileChooserParams) {
if (mUMA != null) {
mUMA.onReceiveValue(null);
}
mUMA = filePathCallback;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
File photoFile = null;
try{
photoFile = createImageFile();
takePictureIntent.putExtra("PhotoPath", mCM);
}catch(IOException ex) {
//Log.e(TAG, "Image file creation failed", ex);
}
if (photoFile != null) {
mCM = "file:" + photoFile.getAbsolutePath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
} else {
takePictureIntent = null;
}
}
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
//contentSelectionIntent.setType("*/*");
contentSelectionIntent.setType("image/*");
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
Intent[] intentArray;
if (takePictureIntent != null) {
intentArray = new Intent[]{takePictureIntent, intent};
} else {
intentArray = new Intent[0];
}
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
chooserIntent.putExtra(Intent.EXTRA_TITLE, getString(R.string.choose_image));
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
startActivityForResult(chooserIntent, FILE_CHOOSER);
return true;
}
});
如果我不将此权限授予AndroidManifest
,此代码将起作用:
<uses-permission android:name="android.permission.CAMERA" />
如果我将此权限添加到清单文件,则相机将无法再使用。
按预期工作。
[仅当您的应用程序在自己的过程中直接或通过库使用相机API时,才需要该权限。您的代码未使用相机API,而是通过ACTION_IMAGE_CAPTURE
调用了某些第三方相机应用。因此,您不需要该权限。
但是,如果您的应用程序does在清单中请求CAMERA
权限,则您必须在运行时以及Android 6.0+设备上都请求它。否则,您也不能使用ACTION_IMAGE_CAPTURE
。
这已覆盖在the documentation中。
在android M +中,首先检查用户是否已授予许可,否则您必须请求它:
private static final String REQUEST_CODE = 123;
if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) != PackageManager.PERMISSION_DENIED) {
requestPermissions(new String[]{Manifest.permission.CAMERA}, REQUEST_CODE);
}