19

I'm calling the default camera from my activity and then handling the onActivityResult. My code seems to work fine on the LG Ally which doesn't have a confirmation when a picture is taken. However, when I run the same app on the Nexus S, it prompts me with an "Ok", "Retake", or "Cancel" before returning to my activity. While "Cancel" works, returning to my activity without saving the picture, "Ok" doesn't seem to have any effect, not even returning to my activity.

My code below:

private void captureImage() {

    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        File path = new File(Environment.getExternalStorageDirectory().getPath() + "/Images/" + (new UserContextAdapter(this)).getUser() + "/");
        path.mkdirs();
        File file = new File(path, "Image_Story_" + mRowId.toString() + ".jpg");

        newImageUri = Uri.fromFile(file);

        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, newImageUri);

        startActivityForResult(intent, CAPTURE_IMAGE);
    }

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {

    switch (requestCode) {
    case CAPTURE_IMAGE:
        switch (resultCode ) {
        case 0:
            Log.i("CAPTURE", "Cancelled by User");
            break;
        case -1:
            mImageUri = newImageUri;
            setImageFromUri();
            }
    }
Rahul Gupta-Iwasaki
  • 1,683
  • 2
  • 21
  • 39

4 Answers4

15

I think I just had the exact same problem.

If the path to save the picture isn't correct, the camera won't return to your app. Once I made sure the directory exists, everything worked fine. Make sure the directory exists, then it should work.

-- Edit --

I just saw, that you call path.mkdirs(); but I think that it doesn't work. As you can read in the android doc "Note that this method does not throw IOException on failure. Callers must check the return value.". Please be sure to check if the directory really exists.

hth

Chad Bingham
  • 32,650
  • 19
  • 86
  • 115
qedejavu
  • 442
  • 6
  • 19
  • hmm, this was so long ago I don't actually remember how I fixed the problem in my case, but thanks for posting your solution @qedejavu ^.^ – Rahul Gupta-Iwasaki Mar 21 '12 at 11:44
  • 2
    Hi Rahul, actually I do check the existence of the folder, and it indeed exists, and the pictures are stored there, but still the OK button doesn't bring me back to my activity. So, what @qedejavu wrote doesn't seem to be the solution. Can you recall how you solved the problem? – Tom Burger Aug 09 '12 at 22:07
  • 2
    This also happens when the native Camera app you don't have access to the directory you're trying to save it to. Your app's private storage is not accessible to the native Camera app, for example. – Joshua Pinter Mar 16 '14 at 23:32
1

Also, make sure your application has <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> if you're using Environment.getExternalStorageDirectory().getPath() above.

Hope this helps =)

Abdo
  • 13,549
  • 10
  • 79
  • 98
1

please check this

Case 1:

Uri newImageUri = null;

File path = new File(Environment.getExternalStorageDirectory().getPath() + "/Images/");

path.mkdirs();

boolean setWritable = false;

setWritable = path.setWritable(true, false);

File file = new File(path, "Image_Story_" + System.currentTimeMillis() + ".jpg");

newImageUri = Uri.fromFile(file);

Log.i("MainActivity", "new image uri to string is " + newImageUri.toString());

Log.i("MainActivity", "new image path is " + newImageUri.getPath());            

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

intent.putExtra(MediaStore.EXTRA_OUTPUT, newImageUri);

startActivityForResult(intent, REQUEST_CODE_CAPTURE_IMAGE);

Case 2:

String fileName = "" + System.currentTimeMillis() + ".jpg";

ContentValues values = new ContentValues();

values.put(MediaStore.Images.Media.TITLE, fileName);

values.put(MediaStore.Images.Media.DESCRIPTION, "Image capture by camera");

values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");

Uri imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

Log.i("MainActivity", "new image uri to string is " + imageUri.toString());

Log.i("MainActivity", "new image path is " + imageUri.getPath());

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

startActivityForResult(intent, REQUEST_CODE_CAPTURE_IMAGE);

I am able to save images through camera on nexus s in both of the above cases In case 1: a.Image is stored in custom folder. b. If the “System.currentTimeMillis()” is changed to (“new Date().toString()”) image is not saved and camera does not return to my activity. (Probably because “System.currentTimeMillis” has no spaces and “new Date().toString()” might be having some special characters and spaces) In case 2: a. Image is stored in camera folder

Thanks to all

bart
  • 304
  • 1
  • 3
  • 13
Satish
  • 320
  • 2
  • 9
0

I have same problem.You have to do only one job,in your Phone storage check that you have Pictures directory or not.If you don't have such library then make it manually.

Hope this works for you.