我正在使用以下代码来捕获图像,该图像在除Android 5.0.1以外的所有版本的android中均能正常工作。请帮助检测代码中的错误。我还添加了该应用所需的所有权限。
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
和onActivityResult如下
private static File getOutputMediaFile(int type) {
// External sdcard location
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
""+R.string.folder_name);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
} else if (type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "VID_" + timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}
public Uri getOutputMediaFileUri(int type) {
return Uri.fromFile(getOutputMediaFile(type));
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// if the result is capturing Image
if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
if (resultCode == -1) {
// successfully captured the image
// display it in image view
BitmapFactory.Options options = new BitmapFactory.Options();
// downsizing image as it throws OutOfMemory Exception for larger
// images
options.inSampleSize = 8;
final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
options);
createComplaint.setPhotoBitmap(bitmap);
imgVPicPhoto.setImageBitmap(bitmap);
} else if (resultCode == 0) {
// user cancelled Image capture
} else {
// failed to capture image
}
}
}
Logcat错误消息-
07-04 18:25:47.513: E/AndroidRuntime(13968): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getPath()' on a null object reference
07-04 18:25:47.513:E / AndroidRuntime(13968):位于com.myaccessibility.newcomplaint.NewPhotoFragment.onActivityResult(NewPhotoFragment.java:379)
07-04 18:25:47.513:E / AndroidRuntime(13968):位于android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:167)
07-04 18:25:47.513:E / AndroidRuntime(13968):位于com.myaccessibility1.MyAccessibility.onActivityResult(MyAccessibility.java:734)
07-04 18:25:47.513:E / AndroidRuntime(13968):位于android.app.Activity.dispatchActivityResult(Activity.java:6543)
07-04 18:25:47.513:E / AndroidRuntime(13968):位于android.app.ActivityThread.deliverResults(ActivityThread.java:4054)
最佳答案
fileUri
是null
。
请记住,您在后台运行时可能会终止过程。这包括您的应用要求其他应用拍照的时间。您需要确保在再次启动过程时可以重建环境。例如,可能需要将fileUri
放入提供给Bundle
的onSaveInstanceState()
中,然后从提供给Bundle
的onRestoreInstanceState()
中恢复。您还应该这样做以应对配置更改,例如用户旋转屏幕。
您可以在文档中阅读有关the process model和handling configuration changes的更多信息。