Skip to content Skip to sidebar Skip to footer

How To Go Back From Calling Intent

I call camera to take a picture. But I cannot go back to my own original activity after taking the picture. What's the problem? Thank you. public void addEntry(View view) {

Solution 1:

It took me a while to get it working and I have made several things and finally it works. I can't tell certainly which of the things I did is the solution to the problem, but they all together form the working solution.

There are multiple reasons why the camera activity does not return back. Two major ones are:

  1. path for the new picture is invalid, or non-existing, or it can't be created

  2. application got suspended and saved path get lost.

So here is my code solving all these problems, all together working.

First I created helper class ImageServices:

classImageServices {

  privatestaticStringgetTempDirectoryPath(Context ctx) {
    File cache;

    // SD Card Mountedif (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
      cache = newFile(Environment.getExternalStorageDirectory().getAbsolutePath() +
              "/Android/data/" + ctx.getPackageName() + "/cache/");
    }
    // Use internal storageelse {
      cache = ctx.getCacheDir();
    }

    // Create the cache directory if it doesn't existif (!cache.exists()) {
      cache.mkdirs();
    }

    return cache.getAbsolutePath(); 
  }

  publicstaticUrigetOutputImageFileUri(Context ctx) {
    // TODO: check the presence of SDCardString tstamp = newSimpleDateFormat("yyyyMMdd_HHmmss").format(newDate());
    File file = newFile(getTempDirectoryPath(ctx), "IMG_" + tstamp + ".jpg");

    returnUri.fromFile(file);

  }
}

The code is partially inspired by developer.android.com and partially by CameraLauncher class of Apache Cordova project.

In my activity the event handler for button to take a picture looks like this:

private Uri imageFileUri;

privatestaticfinalintMAKE_PHOTO_RESULT_CODE=100;
privatestaticfinalintPICK_PHOTO_RESULT_CODE=101;

publicvoidonMakePhoto(View v) {
  Intentintent=newIntent(MediaStore.ACTION_IMAGE_CAPTURE);
  imageFileUri = ImageServices.getOutputImageFileUri(this);
  intent.putExtra(MediaStore.EXTRA_OUTPUT, imageFileUri);
  Log.i("babies", "Taking picture: requested " + imageFileUri);
  startActivityForResult(intent, MAKE_PHOTO_RESULT_CODE);
}

Method onActivityResult does not really contain much, as imageFileUri already points to the existing file and necessary rendering is done in onResume method, which is called when the activity gets back into foreground:

@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
  if (resultCode == RESULT_OK) {
    switch(requestCode) {
      case MAKE_PHOTO_RESULT_CODE:
        assert imageFileUri != null;
        break;
      case ...
        ...other cases...
        break;
    }
  }
}

But this is still not sufficient, as imageFileUri gets lost as your app gets suspended. And on regular device the chances are close to 100%. So next you need to store the value of imageFileUri to instance state:

@OverrideprotectedvoidonSaveInstanceState(Bundle outState) {
  super.onSaveInstanceState(outState);
  if (imageFileUri == null) {
    outState.putString("file-uri", "");
  }
  else {
    outState.putString("file-uri", imageFileUri.toString());
  }
};

and load it again in - easiest is directly in onCreate:

publicvoidonCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  if (savedInstanceState != null) {
    String fileUri = savedInstanceState.getString("file-uri");
    if (!fileUri.equals("")) imageFileUri = Uri.parse(fileUri);
  }
}

So, again, on top of many other solutions presented on this site as well as elsewhere, there are two major differences:

  1. smarter getTempDirectoryPath inspired by Apache Cordova
  2. allowing imageFileUri to survive suspended application

And now - at least for me - everything works fine.

Solution 2:

Answer

Use appContext.getExternalCacheDir() and don't forget to mention permissons.

    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
              if (requestCode == TAKE_PICTURE) 
              {
                      if(resultCode==Activity.RESULT_OK)
                  { //if (data != null)//{   
                    Toast.makeText(this, "Successfully Registered!", Toast.LENGTH_LONG);
                    ImageView Registerimage= (ImageView)findViewById(R.id.RegisterPicture);
                    Registerimage.setImageURI(registeryFileUri);
                    //}
                       }
                      else
                         Toast.makeText(this, "Not Registered!", Toast.LENGTH_LONG);
             }  
**"android.permission.CAMERA"**     

Check whether the above permission is specified in your manifest or not

Note: It's better to usegetExternalCacheDir() than getFilesDir() if you still dont get the 
      image then use that. Dont forgot to specify the permission "android.permission.WRITE_EXTERNAL_STORAGE"if you use the getExternalCacheDir().

Solution 3:

On some devices data will unfortunately be null in onActivityResult after calling the camera activity. So you may need to save your state in your activity's variables, and them read them in onActivityResult. Be sure to save these variables in onSaveInstanceState and restore them in onCreate.

Post a Comment for "How To Go Back From Calling Intent"