我正在使用一个可以调用设备相机的按钮。
private void captureImage() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(ImageUtils.MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}
public Uri getOutputMediaFileUri(int type) {
item = ImageUtils.getOutputMediaFile(type, getActivity());
return item.getIcon();
}
和getOutputMediaFile()方法是,
public static ImageItem getOutputMediaFile(int type, Activity activity) {
// External sdcard location
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
IMAGE_DIRECTORY_NAME);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
+ IMAGE_DIRECTORY_NAME + " directory");
return null;
}
}
// Create a media file name
Date date = new Date();
String timeStamp = CommonUtils.dateToString(date, "yyyyMMddHHmmss");
String dateString = CommonUtils.dateToString(date,
"yyyy/MM/dd HH:mm:ss");
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
}
else {
return null;
}
ImageItem item = new ImageItem();
item.setId(timeStamp);
item.setmIcon(Uri.fromFile(mediaFile));
item.setDate(dateString);
item.setmSpans(1);
// save image in on activity result
return item;
}
在同一个活动上我为图像结果调用OnActivityResult方法,
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE
&& resultCode == RESULT_OK) {
String[] fileColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getActivity().getContentResolver().query(fileUri,
fileColumn, null, null, null);
String contentPath = null;
if (cursor.moveToFirst()) {
contentPath = cursor.getString(cursor
.getColumnIndex(fileColumn[0]));
horseImage.setImageBitmap(ImageManager
.getImage(new ImageManagerRequest(contentPath)));
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(getActivity(), "Capture Cancelled",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity(), "Capture failed",
Toast.LENGTH_LONG).show();
}
}
}
运行此代码时,我收到以下错误日志。
09-24 19:17:10.250: E/AndroidRuntime(16995): FATAL EXCEPTION: main
09-24 19:17:10.250: E/AndroidRuntime(16995): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=198496, result=-1, data=null} to activity {com.eyepax.horseapp/com.eyepax.horseapp.ui.activities.MainActivity}: java.lang.NullPointerException
09-24 19:17:10.250: E/AndroidRuntime(16995): at android.app.ActivityThread.deliverResults(ActivityThread.java:3141)
09-24 19:17:10.250: E/AndroidRuntime(16995): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3184)
09-24 19:17:10.250: E/AndroidRuntime(16995): at android.app.ActivityThread.access$1100(ActivityThread.java:130)
09-24 19:17:10.250: E/AndroidRuntime(16995): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
09-24 19:17:10.250: E/AndroidRuntime(16995): at android.os.Handler.dispatchMessage(Handler.java:99)
09-24 19:17:10.250: E/AndroidRuntime(16995): at android.os.Looper.loop(Looper.java:137)
09-24 19:17:10.250: E/AndroidRuntime(16995): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-24 19:17:10.250: E/AndroidRuntime(16995): at java.lang.reflect.Method.invokeNative(Native Method)
09-24 19:17:10.250: E/AndroidRuntime(16995): at java.lang.reflect.Method.invoke(Method.java:511)
09-24 19:17:10.250: E/AndroidRuntime(16995): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-24 19:17:10.250: E/AndroidRuntime(16995): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-24 19:17:10.250: E/AndroidRuntime(16995): at dalvik.system.NativeStart.main(Native Method)
09-24 19:17:10.250: E/AndroidRuntime(16995): Caused by: java.lang.NullPointerException
09-24 19:17:10.250: E/AndroidRuntime(16995): at com.eyepax.horseapp.ui.activities.AddHorseBasicInfo.onActivityResult(AddHorseBasicInfo.java:270)
09-24 19:17:10.250: E/AndroidRuntime(16995): at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:167)
09-24 19:17:10.250: E/AndroidRuntime(16995): at android.app.Activity.dispatchActivityResult(Activity.java:5192)
09-24 19:17:10.250: E/AndroidRuntime(16995): at android.app.ActivityThread.deliverResults(ActivityThread.java:3137)
09-24 19:17:10.250: E/AndroidRuntime(16995): ... 11 more
fileUri是一个全局变量。我经历了很多例子,但我无法解决我的问题。许多例子都是因为Intent数据变为null而发生这种情况。然后我将onActivityResult更改为如下,但随后它就会逃脱。
if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE && resultCode == RESULT_OK
&& null != data) {}
帮我解决这个问题。
答案 0 :(得分:0)
图像的Uri应包含在onActivityResult的data.getData()中
在一个旧项目中,我也使用了这个:
public static Uri getLastPhoto(Context context) {
String[] columns = {MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DATE_ADDED};
ContentResolver cr = context.getContentResolver();
Cursor cursor = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null,
MediaStore.MediaColumns.DATE_ADDED + " DESC");
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String path = cursor.getString(columnIndex);
cursor.close();
return Uri.fromFile(new File(path));
}
答案 1 :(得分:0)
这在许多设备中都会发生。它由于一些操作系统问题。如果您传递要保存的图像的名称,并使用它进行检索,则会更好。
capturedImagePath = MyApplication.cameraImageDirectory + "/camera"
+ num + ".png";
File mFileTemp = new File(capturedImagePath);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
Uri mImageCaptureUri = null;
String state = Environment.getExternalStorageState();
mImageCaptureUri = Uri.fromFile(mFileTemp);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
mImageCaptureUri);
intent.putExtra("return-data", true); //$NON-NLS-1$
startActivityForResult(intent, REQUEST_CODE_TAKE_PICTURE);
} catch (ActivityNotFoundException e) {
Log.d("camera fieldtip", "cannot take picture", e); //$NON-NLS-1$
}