Reputation: 3614
I'm trying to get a picture from user and then send it to our server. I provide two options: choosing from gallery or taking a new picture using default camera in their phones.
Gallery thing works correctly, I can get the photo they selected and send it without any problem. But in camera option, camera opens, takes a picture, saves it on SD card with the location I specified, no problem, but when it returns back to my application, I always get a NullPointerException and my application is forced to close. Here is what I do:
private String tempFilePath = "";
...
public void addPhotoUsingCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
tempFilePath = getOutputMediaFilePath(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(tempFilePath)));
startActivityForResult(intent, CAMERA_ACTIVITY_CODE);
}
And then I use following to get the photo taken:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
...
case CAMERA_ACTIVITY_CODE:
if (resultCode == Activity.RESULT_OK) {
picture = new File(tempFilePath);
}
break;
...
}
}
I also tried
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image = (ImageView) findViewById(R.id.photoTaken);
image.setImageBitmap(thumbnail);
inside case CAMERA_ACTIVITY_CODE:
but no matter what I try I always get a NullPointerException in this part of code.
There is the logcat info where line 202 corresponds to case CAMERA_ACTIVITY_CODE:
part.
01-31 23:40:13.154: ERROR/AndroidRuntime(8009): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=101, result=-1, data=null} to activity {com.ollaa.android/com.ollaa.android.share.ShareFinalScreen}: java.lang.NullPointerException
01-31 23:40:13.154: ERROR/AndroidRuntime(8009): Caused by: java.lang.NullPointerException
01-31 23:40:13.154: ERROR/AndroidRuntime(8009): at com.ollaa.android.share.ShareFinalScreen.onActivityResult(ShareFinalScreen.java:202)
Any help is greatly appreciated.
Note: I have declared all necessary permissions in AndroidManifest related to camera and SD card.
EDIT: I realized that tempFilePath does not last until camera activity returns to my app. I put the initialization for tempFilePath in my onCreate
method but this time picture is not set when I try to reach it from an outside function other than onActivityResult
. I feel like I do not know Java at all, when we change a class variable within a function, the changed value should be visible from all functions of that class right??
Upvotes: 1
Views: 2297
Reputation: 1687
When using:
intent.putExtra(MediaStore.EXTRA_OUTPUT, myFile);
The file must exist on the 'disk'. The Camera Activity does not create it, which is IMHO a bug. So you have to use myFile.createNewFile();
Also, You have to send Camera Activity a file and not a path.
Try something like this:
tempFilePath = getOutputMediaFilePath(MEDIA_TYPE_IMAGE);
String fileName = "myPhoto";
File myFile = new File (tempFilePath,fileName);
myFile.createNewFile();
// at this point stop the debugger and check if the file exists on the 'disk'.
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(myFile)));
There may be a better way to do this without using MediaStore.EXTRA_OUTPUT
. Maybe another user can help.
Also, note if the user presses cancel and does not take a picture you have to delete the file.
Note: I am using Android API 2.2.
I hope this helps.
Upvotes: 2