我正在使用下面的代码来捕获它在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):at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:167) (E)/ AndroidRuntime(13968):at android.app.Activity.dispatchActivityResult(Activity.java:6543) 07-04 18:25:47.513:E/AndroidRuntime(13968):在android.app.ActivityThread.deliverResults(ActivityThread.java:4054)
使用LogCat检查与您的崩溃相关的Java堆栈跟踪:https://stackoverflow.com/questions/23353173/uncomfort-myapp-has-stopped-how-can-i-solve-this – CommonsWare
可能的重复[ Android的startCamera给我空意图和...它会毁了我的全局变量?](http://stackoverflow.com/questions/20424909/android-startcamera-gives-me-null-intent-and-does-it-destroy -my-global-varia) –