Android camera activity doesn't return to my app when the OK button is pressed

I have an app that’s fully working. It just has 1 problem with my Camera Intent or permissions.

The app is supposed to launch the Camera Activity when the user presses a button. That’s working fine, but when the user accepts a picture by clicking the OK button on the camera, it doesn’t come back to my app. If they press the cancel button on the camera, it comes back to my app as expected.

  • Gradle version 1.10 is required. Current version is 2.2.1. in android
  • Reload activity in Android
  • Android: What is Binder Thread?
  • In a nutshell what's the difference from using OAuth2 request getAuthToken and getToken
  • Custom ListPreference with dynamic entries
  • Google Map Android Api V2 Sample Code not working
  • I have read all the similar questions I can find on here, but none of them have fixed my problem. I need to tell the camera exactly where to save the image because I want to alternate between 2 in my app. Here’s the code that creates the Camera Intent and starts it:

    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.parse(imagePath));
    startActivityForResult(intent, 11);
    

    I have tried multiple values for imagePath (which is a String), but the camera’s OK button has not worked with any of them. These are the paths I’ve tried:

    /mnt/sdcard/<appName>/cameraImages/image1.jpg
    from Environment.getExternalStorageDirectory()
    
    /mnt/sdcard/Android/data/<appPkg>/cache/<appName>/cameraImages/image1.jpg
    from context.getExternalCacheDir()
    
    /mnt/sdcard/Android/data/<appPkg>/cache/<appName>/cameraImages/image1.jpg
    from context.getCacheDir()
    

    I have the following permissions in my manifest file:

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" android:required="false" />
    <uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />
    <uses-feature android:name="android.hardware.camera.flash" android:required="false" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    

    Can you guys help me figure out what’s wrong with my code, or tell me what a valid path could be? I prefer if the image is stored in a cache location because it’s not important for the image to continue existing after the user leaves the application, but I don’t really care who has access to the image.

    I’m doing all testing on the 4.1 emulator, compiling with the 4.1 SDK, and my minimum version is API 8 (2.2).

    I have put a case for the intent in onActivityResult(), but I don’t think you guys need to see that code because it shouldn’t be affecting the problem.

    Related posts:

    Set selected item of spinner programmatically
    How to handle long tap on ListView item?
    Error opening mobile network settings menu
    Is there a way to pass parameters to the marketplace installer for an app
    How can I know that OnResume comes after onCreate?
    How to update the PNR Capcha added in the Indian Railways Site
  • How to do independently scrolling rows and columns (a la Netflix app) on Android in a WebView?
  • Android - writing/saving files from native code only
  • What is createChooser when I have to use Intents? What I could do with this method?
  • Firebase Dynamic Links not deep linking with short URL on Custom tabs
  • Captive portal on android device
  • How to change ActionBar menu background color (android 4 & 5)
  • 2 Solutions collect form web for “Android camera activity doesn't return to my app when the OK button is pressed”

    I was able to figure out the issue. It seems that instead of using Uri.parse(imagePath), you have to use Uri.fromFile(new File(imagePath)) when you add the extra. I’ve put the new Intent code below in case it helps anyone with the same problem.

    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(imagePath)));
    startActivityForResult(intent, 11);
    

    I am now successfully using context.getExternalCacheDir() to create the full path imagePath. The resulting path that I created is /mnt/sdcard/Android/data/<appPkg>/cache/<appName>/cameraImages/image1.jpg.

    The problem is that you need to share hare your local (Application’s internal) file to the camera activity. Creating a Content Provider to this effect is the ONLY thing that worked for me. See the second option here:
    http://dharmendra4android.blogspot.com/2012/04/save-captured-image-to-applications.html

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.