0

ALL,

I have following code:

    try
    {
        Intent captureIntent = new Intent( android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
        File storage = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES );
        cameraImageFiles = File.createTempFile( "user_photo", ".png", storage );
        captureIntent.putExtra( MediaStore.EXTRA_OUTPUT, Uri.fromFile( cameraImageFiles ) );
        final Intent galleryIntent = new Intent();
        galleryIntent.setType( "image/*" );
        galleryIntent.setAction( Intent.ACTION_GET_CONTENT );
        Intent photoIntent = Intent.createChooser( galleryIntent, "Select or take a new picture" );
        photoIntent.putExtra( Intent.EXTRA_INITIAL_INTENTS, new Intent[] { captureIntent } );
        startActivityForResult( photoIntent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE );
    }
    catch( IOException e )
    {
        Utils.displayErrorDialog( context, e.getMessage() );
    }

Every example on the web and here talk about retrieving the image from camera when there is only 1 intent.

Here I have an IntentChooser to select between gallery image or taking the new picture with camera. As far as I understand using this code I will not be able to simply get the image since in "onActivityResult()" I should save the picture to the file - it will not be saved automatically.

Now what I'd like to do is to get the image from the camera shot. I don't really care whether it will be saved or not - I just want a photo.

I know how to get the image from gallery - I have this code and it works. But getting the image from the camera is a puzzle right now.

Thank you.

[EDIT] This is the code I put in the onActivityResult()

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if( requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE )
    {
        if( resultCode == RESULT_OK )
        {
            Uri uri = null;
            if( data == null )
            {
                if( cameraImageFiles.exists() )
                {
                    uri = Uri.fromFile( cameraImageFiles );
                    InputStream input;
                    try
                    {
                        input = getContentResolver().openInputStream( uri );
                        BitmapFactory.Options opts = new BitmapFactory.Options();
                        opts.inJustDecodeBounds = true;
                        bmp = BitmapFactory.decodeStream( input, null, opts );
                        input.close();
                        int height = opts.outHeight;
                        int width = opts.outWidth;
                        int inSampleSize = 1;
                        int reqHeight = camera.getHeight();
                        int reqWidth = camera.getWidth();
                        if( height > reqHeight || width > reqWidth )
                        {
                            int halfHeight = height / 2;
                            int halfWidth = width / 2;
                            while( ( halfHeight / inSampleSize ) > reqHeight && ( halfWidth / inSampleSize ) > reqWidth )
                                inSampleSize *= 2;
                        }
                        opts.inSampleSize = inSampleSize;
                        opts.inJustDecodeBounds = false;
                        bmp = BitmapFactory.decodeStream( input, null, opts );
                        camera.setImageBitmap( bmp );
                    }
                    catch( FileNotFoundException e )
                    {
                        Utils.displayErrorDialog( this, e.getMessage() );
                    }
                    catch( IOException e )
                    {
                        Utils.displayErrorDialog( this, e.getMessage() );
                    }
                }
                photoReady = true;
            }

After executing, the code gives null as bmp, so I guess the image is not saved.

[/EDIT]

Igor
  • 5,620
  • 11
  • 51
  • 103
  • It has to be saved. You cannot get the photo otherwise. – CommonsWare May 16 '14 at 17:27
  • you need to create your own camera using a surfaceview. – njzk2 May 16 '14 at 17:43
  • Why make an intent to select between galery or camera? This doesnt make sense, why dont you put up a dialog and either launch camera or gallery on the dialog result – Tascalator May 16 '14 at 17:47
  • @CommonsWare, do you have a coding example? As far as I understand saving should occur in the "onActivityResult()" and then I can retrieve it. Am I right? But if yes, how do I save it? I don't have anything in that function. The Intent that will be passed there will be NULL... – Igor May 16 '14 at 20:36
  • @Tascalator, because its a native way of doing this. What if there is more applications to get the image to be displayed? – Igor May 16 '14 at 20:37
  • "But if yes, how do I save it?" -- it has already been saved, by the third-party camera app, wherever you told it to put the image. See https://github.com/commonsguy/cw-omnibus/tree/master/Camera/Content – CommonsWare May 16 '14 at 20:48
  • @CommonsWare, please see my edit to the question. Thank you. – Igor May 16 '14 at 21:17

1 Answers1

0

Camera will save the captured picture on disk, but it won't be a PNG, it will be Jpeg. Also, data willl not be null if you use ACTION_IMAGE_CAPTURE, so your code will not work at all. Furthermore, camera capture intent should be launched carefully to avoid a well documented bug that hunts many devices. See https://stackoverflow.com/a/16433874/192373 and around.

Note that ACTION_GET_CONTENT requires special treatment on KitKat, you might be better served with ACTION_OPEN_DOCUMENT, see https://stackoverflow.com/a/20177611/192373

Community
  • 1
  • 1
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • OK, so I'm better off with my own dialog for the action selection? Because it looks like there is too much trouble with the standard one.... – Igor May 18 '14 at 04:07
  • I don't think the system chooser dialog makes your life harder. Even if you open your own dialog, you still need to handle correctly both actions, and be prepared to changes introduced in Kit-Kat. – Alex Cohn May 18 '14 at 05:32
  • So let me reiterate. After picture is taken the system will save it by itself to the file with extension ".jpeg". I don't need to save it in onActivityResult(), I just need to load this file into memory. Now the changes on Kit-Kat, are they backward compatible? Meaning that I will be able to use the code on the previous versions.... – Igor May 18 '14 at 09:10
  • And I'm not sure about this: "Also data will not be null if you use ACTION_IMAGE_CAPTURE...". When testing this is exactly what happened - using the camera to take the shot, onActivityResult() last parameter is null, when selecting the image from Gallery - it is not. – Igor May 18 '14 at 09:12
  • "using the camera to take the shot, onActivityResult() last parameter is null" - that's strange. What device/system ver did you try? – Alex Cohn May 18 '14 at 14:13
  • The [discussion](http://stackoverflow.com/a/20177611/192373) explains how the KitKat changes can be treated correctly at runtime. – Alex Cohn May 18 '14 at 14:17
  • I have Samsung Galaxy with Android version 4.2.2. I also have LG phone with Android version 2.2 – Igor May 18 '14 at 22:31
  • I changed the extension to become "jpeg", but still the bmp object is NULL. – Igor May 18 '14 at 22:59
  • It's not only the extension; the camera app cannot access the location you provide as EXTRA_OUTPUT with the intent. – Alex Cohn May 19 '14 at 03:56
  • well I do have WRITE_EXTERNAL_STORAGE permission. What else do I miss? – Igor May 19 '14 at 09:42
  • I really do not know what specifically is wrong with your intent. I can only tell that the correct one creates a picture somewhere, and brings a thumbnail to `onActivityResult()`. I suggest that you take some working example, and step by step modify it to fit your needs. If you start with code that does not work, it is much harder to find the offending line/ command/ setting. – Alex Cohn May 19 '14 at 09:50
  • Yes, I actually put a link to it in the answer: http://stackoverflow.com/a/1932268/192373. You can also try http://androidcookbook.com/Recipe.seam?recipeId=380 or even http://developer.android.com/training/camera/photobasics.html – Alex Cohn May 20 '14 at 15:50