Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

like image 235
cmasupra Avatar asked Oct 10 '12 04:10

cmasupra


2 Answers

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.

like image 88
cmasupra Avatar answered Nov 03 '22 11:11

cmasupra


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

like image 34
Mike6679 Avatar answered Nov 03 '22 09:11

Mike6679