Reputation: 10179
So I managed to create a chooser intent to pick image from gallery or take a photo. The problem I am facing is that when I choose Camera and take a photo on returning back to my activity I receive the error that my application has stopped working unexpectedly whereas when I am using my gallery or google+ photos or file explorer then it's working totally fine. I am not able to figure out the error. Following is my code of starting the intent and handling the activity result:
Function that starts the intent, called on the click of an ImageView:
private void openImageIntent() {
// Determine Uri of camera image to save.
final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "MyDir" + File.separator);
root.mkdirs();
final String fname = "img_" + System.currentTimeMillis() + ".jpg";
final File sdImageMainDirectory = new File(root, fname);
outputFileUri = Uri.fromFile(sdImageMainDirectory);
// Camera.
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for(ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
cameraIntents.add(intent);
}
// Filesystem.
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
startActivityForResult(chooserIntent, 0);
}
The onActivityResult
method:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent returnIntent) {
super.onActivityResult(resultCode, requestCode, returnIntent);
if(requestCode == 0) {
if(resultCode == RESULT_OK) {
final boolean isCamera;
if(returnIntent == null)
{
isCamera = true;
}
else
{
final String action = returnIntent.getAction();
if(action == null)
{
isCamera = false;
}
else
{
isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
}
}
Uri selectedImageUri;
if(isCamera)
{
selectedImageUri = outputFileUri;
ivProfilePicture.setImageURI(selectedImageUri); //Troublesome
}
else
{
selectedImageUri = returnIntent == null ? null : returnIntent.getData();
ivProfilePicture.setImageURI(selectedImageUri); //Perfectly fine.
}
}
}
}
I do not know why is this happening, any help would be appreciated. Thanks.
And of course, the logcat:
01-29 16:19:40.886: E/AndroidRuntime(14234): FATAL EXCEPTION: main 01-29 16:19:40.886: E/AndroidRuntime(14234): java.lang.RuntimeException: Unable to resume activity {com.footstapps.letsmeet/com.footstapps.letsmeet.ProfileInfoActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {com.footstapps.letsmeet/com.footstapps.letsmeet.ProfileInfoActivity}: java.lang.NullPointerException 01-29 16:19:40.886: E/AndroidRuntime(14234): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2797) 01-29 16:19:40.886: E/AndroidRuntime(14234): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2826) 01-29 16:19:40.886: E/AndroidRuntime(14234): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269) 01-29 16:19:40.886: E/AndroidRuntime(14234): at android.app.ActivityThread.access$600(ActivityThread.java:144) 01-29 16:19:40.886: E/AndroidRuntime(14234): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1259) 01-29 16:19:40.886: E/AndroidRuntime(14234): at android.os.Handler.dispatchMessage(Handler.java:99) 01-29 16:19:40.886: E/AndroidRuntime(14234): at android.os.Looper.loop(Looper.java:137) 01-29 16:19:40.886: E/AndroidRuntime(14234): at android.app.ActivityThread.main(ActivityThread.java:5140) 01-29 16:19:40.886: E/AndroidRuntime(14234): at java.lang.reflect.Method.invokeNative(Native Method) 01-29 16:19:40.886: E/AndroidRuntime(14234): at java.lang.reflect.Method.invoke(Method.java:525) 01-29 16:19:40.886: E/AndroidRuntime(14234): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 01-29 16:19:40.886: E/AndroidRuntime(14234): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 01-29 16:19:40.886: E/AndroidRuntime(14234): at dalvik.system.NativeStart.main(Native Method) 01-29 16:19:40.886: E/AndroidRuntime(14234): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {com.footstapps.letsmeet/com.footstapps.letsmeet.ProfileInfoActivity}: java.lang.NullPointerException 01-29 16:19:40.886: E/AndroidRuntime(14234): at android.app.ActivityThread.deliverResults(ActivityThread.java:3374) 01-29 16:19:40.886: E/AndroidRuntime(14234): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2784) 01-29 16:19:40.886: E/AndroidRuntime(14234): ... 12 more 01-29 16:19:40.886: E/AndroidRuntime(14234): Caused by: java.lang.NullPointerException 01-29 16:19:40.886: E/AndroidRuntime(14234): at com.footstapps.letsmeet.ProfileInfoActivity.onActivityResult(ProfileInfoActivity.java:160) 01-29 16:19:40.886: E/AndroidRuntime(14234): at android.app.Activity.dispatchActivityResult(Activity.java:5322) 01-29 16:19:40.886: E/AndroidRuntime(14234): at android.app.ActivityThread.deliverResults(ActivityThread.java:3370) 01-29 16:19:40.886: E/AndroidRuntime(14234): ... 13 more
EDIT:
I debugged the application and initialized the selectedImageUri
with Uri.EMPTY
. This removed the error but now the ImageView is not getting update however it's updated when I select image from gallery.
Upvotes: 3
Views: 1056
Reputation: 20500
So apparently Android was clearing the memory or opening a new activity instead of resuming the last one. Saving the current state of the activity would solve this problem. For this example you need to save the variable that has the uri of the image.
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putString("outputFileUri",outputFileUri);
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
outputFileUri = savedInstanceState.getString("outputFileUri");
}
Since onRestoreInstanceState
will be called before onActivityResult
the value of outputFileUri
will not be null.
Upvotes: 3