У меня проблема с захватом изображения с камеры через веб-просмотр.Первая проблема - приложение закрывается, когда я отменяю процесс выбора файла из галереи или захвата изображения с камеры.Во-вторых, когда я хочу сохранить изображение с камеры, чтобы загрузить его, приложение принудительно закрывает показ «Попытка вызвать виртуальный метод» android.content.ClipData android.content.Intent.getClipData () 'для ссылки на нулевой объект "
это код, в котором произошла ошибка
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode != INPUT_FILE_REQUEST_CODE || mUploadMessage == null){
super.onActivityResult(requestCode, resultCode, data);
return;
}
try{
String file_path = mCameraPhotoPath.replace("file:", "");
File file = new File(file_path);
size = file.length();
}catch(Exception e){
Log.e("Error!", "Error while opening image file" + e.getLocalizedMessage());
}
if (data != null || mCameraPhotoPath != null) {
Integer count = 1;
ClipData images = null;
try {
images = data.getClipData();
} catch (Exception e) {
Log.e("Error!", e.getLocalizedMessage());
}
if (images != null && data != null && data.getDataString() != null) {
count = data.getDataString().length();
} else if (images != null) {
count = images.getItemCount();
} else {
Toast.makeText(getApplicationContext(),
"images = null / data = null",
Toast.LENGTH_LONG).show();
}
Uri[] results = new Uri[count];
if (resultCode == Activity.RESULT_OK){
if (data == null || data.getDataString() == null){
//Log.d(Activity.class.getSimpleName(), "Executed");
//if there is no data, proceed to camera
if (mCameraPhotoPath != null){
results = new Uri[]{Uri.parse(mCameraPhotoPath)};
//check whether this line is executed or not
//Log.d(Activity.class.getSimpleName(), "Executed");
} else {
Toast.makeText(getApplicationContext(),
"images = null / data = null",
Toast.LENGTH_LONG).show();
}
} else {
String dataString = data.getDataString();
if (dataString != null){
results = new Uri[]{Uri.parse(dataString)};
} else {
Toast.makeText(this, "Error occured!", Toast.LENGTH_SHORT).show();
Log.e(Activity.class.getSimpleName(), "if(datastring!=null)");
}
}
}
mUploadMessage.onReceiveValue(results);
mUploadMessage = null;
}
}
Метод createImageFile ()
private File createImageFile() throws IOException {
//Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_"+timeStamp+"_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES
);
File imageFile = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
return imageFile;
}
и fileChooser в методе WebChromeClient
//for android 5.0+
public boolean onShowFileChooser(WebView view, ValueCallback<Uri[]> filePath, WebChromeClient.FileChooserParams fileChooserParams){
//double check that we don't have any existing callbacks
if (mUploadMessage != null){
mUploadMessage.onReceiveValue(null);
} else {
Toast.makeText(getApplicationContext(),
"mUploadMessage = null",
Toast.LENGTH_LONG).show();
}
mUploadMessage = filePath;
Log.e("FileChooserParams => ", filePath.toString());
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null){
//create file where the photo should go
File photoFile = null;
try{
photoFile = createImageFile();
takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);
} catch(IOException ex){
//Error occured while creating the file
Log.e(TAG, "Unable to create image file", ex);
}
//continue only if the File was succesfully created
if (photoFile != null){
mCameraPhotoPath = "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.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
contentSelectionIntent.setType("image/*");
Intent[] intentArray;
if (takePictureIntent != null){
intentArray = new Intent[]{takePictureIntent};
} else {
intentArray = new Intent[0];
}
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, 1);
return true;
}