我正在创建一个应用程序以在Web视图中加载URL。我创建了“图像选择器”以开始从画廊和相机中选择照片的活动。
A)从图库中选择图像工作正常。B)当我通过摄像机捕获图像时,它在Web视图中显示崩溃的小图像图标。
注意:在拍摄图像后,当我在“相机”屏幕上停留5-8秒以上时,相机照片在网络视图上完美显示。但是,即使在捕获图像后迅速按“是”按钮也应该可以。
这是我的Web视图代码,用于设置WebChromeClient
webView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onShowFileChooser(
WebView webView, ValueCallback<Uri[]> newFilePathCallback,
FileChooserParams fileChooserParams) {
if (filePathCallback != null) {
filePathCallback.onReceiveValue(null);
}
filePathCallback = newFilePathCallback;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
Log.d("Mainn", "onShowFileChooser()");
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException e) {
Log.d("Mainn", "onShowFileChooser().. e : " + e.getLocalizedMessage());
e.printStackTrace();
}
Log.d("Mainn", "onShowFileChooser() 4");
// Continue only if the File was successfully created
if (photoFile != null) {
Log.d("Mainn", "onShowFileChooser() 5");
cameraPhotoPath = "file:" + photoFile.getAbsolutePath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
} else {
Log.d("Mainn", "onShowFileChooser() 6");
takePictureIntent = null;
}
}
Log.d("Mainn", "onShowFileChooser() 7");
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
contentSelectionIntent.setType("image/*");
Intent[] intentArray;
if (takePictureIntent != null) {
Log.d("Mainn", "onShowFileChooser() 8");
intentArray = new Intent[]{takePictureIntent};
} else {
Log.d("Mainn", "onShowFileChooser() 9");
intentArray = new Intent[0];
}
Log.d("Mainn", "onShowFileChooser() 10");
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
startActivityForResult(chooserIntent, INPUT_FILE_REQUEST_CODE);
return true;
}
});
}
onActivityResult()代码在这里
@Override
public void onActivityResult(int requestCode, int resultCode, final Intent data) {
if (requestCode != INPUT_FILE_REQUEST_CODE || filePathCallback == null) {
super.onActivityResult(requestCode, resultCode, data);
return;
}
Uri[] results = null;
// Check that the response is a good one
if (resultCode == Activity.RESULT_OK) {
if (data == null) {
// If there is not data, then we may have taken a photo
if (cameraPhotoPath != null) {
results = new Uri[]{Uri.parse(cameraPhotoPath)};
}
} else {
String dataString = data.getDataString();
if (dataString != null) {
results = new Uri[]{Uri.parse(dataString)};
}
}
}
filePathCallback.onReceiveValue(results);
filePathCallback = null;
}
使用FileProvider代替Uri.fromFile()。它将解决此问题。
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri uri = FileProvider.getUriForFile(getActivity(), getActivity().getPackageName(), photoFile);
takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);