我有一个Fragment
可以拍照的。一些片段有一张普通的图片,还有一张额外的图片。这意味着我使用两个不同RESULT CODES
的照片来拍摄我正在拍摄的照片。我得到的错误只是当我使用RESULT_PICTURE_EXTRA
代码时,它似乎是随机的。
为什么我在使用时会收到此错误REQUEST_PICTURE_EXTRA
?另外,如果有帮助,我会在 a 中显示这张图片,PopupWindow
但它没有显示出来。
片段代码
private static final int REQUEST_PICTURE = 1;
private static final int REQUEST_PICTURE_EXTRA = 2;
private void showExtraPicture() {
ImageView extraImage = (ImageView) view.findViewById(R.id.extra_image);
if (step.getIsReqPictureFinished()) {
try {
FileInputStream fis = getActivity().openFileInput(step.getExtraImageFilename());
Bitmap imgFromFile = BitmapFactory.decodeStream(fis);
fis.close();
extraImage.setImageBitmap(imgFromFile);
extraImage.invalidate();
}
catch (FileNotFoundException e) { e.printStackTrace(); }
catch (IOException e) { e.printStackTrace(); }
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_PICTURE && resultCode == Activity.RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap image = (Bitmap) extras.get("data");
ImageHandler imageHandler = new ImageHandler(getActivity());
String filename = imageHandler.writeToFile(image, step.getChecklistId(), step.getOrder(), false);
step.setImageFilename(filename);
finishStep();
showResult();
checkIfAllFinished();
}
if (requestCode == REQUEST_PICTURE_EXTRA && resultCode == Activity.RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap image = (Bitmap) extras.get("data");
ImageHandler imageHandler = new ImageHandler(getActivity());
String filename = imageHandler.writeToFile(image, step.getChecklistId(), step.getOrder(), true);
step.setExtraImageFilename(filename);
if (step.getReqPicture()) { step.setIsReqPictureFinished(true); }
showExtraPicture();
checkIfAllFinished();
}
}
堆栈跟踪
01-31 14:42:54.192: E/AndroidRuntime(21536): FATAL EXCEPTION: main
01-31 14:42:54.192: E/AndroidRuntime(21536): java.lang.RuntimeException: Failure delivering result ResultInfo{who=android:fragment:0, request=2, result=-1, data=Intent { act=inline-data dat=content://media/external/images/media/3504 (has extras) }} to activity {com.medusa.checkit.android/com.medusa.checkit.android.StepActivity}: java.lang.NullPointerException
01-31 14:42:54.192: E/AndroidRuntime(21536): at android.app.ActivityThread.deliverResults(ActivityThread.java:3205)
01-31 14:42:54.192: E/AndroidRuntime(21536): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3248)
01-31 14:42:54.192: E/AndroidRuntime(21536): at android.app.ActivityThread.access$1200(ActivityThread.java:143)
01-31 14:42:54.192: E/AndroidRuntime(21536): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
01-31 14:42:54.192: E/AndroidRuntime(21536): at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 14:42:54.192: E/AndroidRuntime(21536): at android.os.Looper.loop(Looper.java:137)
01-31 14:42:54.192: E/AndroidRuntime(21536): at android.app.ActivityThread.main(ActivityThread.java:4950)
01-31 14:42:54.192: E/AndroidRuntime(21536): at java.lang.reflect.Method.invokeNative(Native Method)
01-31 14:42:54.192: E/AndroidRuntime(21536): at java.lang.reflect.Method.invoke(Method.java:511)
01-31 14:42:54.192: E/AndroidRuntime(21536): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
01-31 14:42:54.192: E/AndroidRuntime(21536): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
01-31 14:42:54.192: E/AndroidRuntime(21536): at dalvik.system.NativeStart.main(Native Method)
01-31 14:42:54.192: E/AndroidRuntime(21536): Caused by: java.lang.NullPointerException
01-31 14:42:54.192: E/AndroidRuntime(21536): at com.medusa.checkit.android.StepFragment.showExtraPicture(StepFragment.java:388)
01-31 14:42:54.192: E/AndroidRuntime(21536): at com.medusa.checkit.android.StepFragment.onActivityResult(StepFragment.java:547)
01-31 14:42:54.192: E/AndroidRuntime(21536): at android.app.Activity.dispatchActivityResult(Activity.java:5367)
01-31 14:42:54.192: E/AndroidRuntime(21536): at android.app.ActivityThread.deliverResults(ActivityThread.java:3201)
01-31 14:42:54.192: E/AndroidRuntime(21536): ... 11 more