Picture coming from camera or gallery?

Hossam Oukli picture Hossam Oukli · May 6, 2013 · Viewed 23.1k times · Source

I have an intent chooser that allows me to pick image from gallery or camera like this:

    Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT,null);
    galleryIntent.setType("image/*");
    galleryIntent.addCategory(Intent.CATEGORY_OPENABLE);

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


    Intent chooser = new Intent(Intent.ACTION_CHOOSER);
    chooser.putExtra(Intent.EXTRA_INTENT, galleryIntent);      
    chooser.putExtra(Intent.EXTRA_TITLE, "title");

    Intent[] intentArray =  {cameraIntent}; 
    chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
    startActivityForResult(chooser,REQUEST_CODE);

I want my onActivityResult method to be like this:

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if(Condition == picture_coming_from_gallery)
    {
     //my code here
    }
    else if(condition == picture_coming_from_camera)
    {
     //another code here
    }
}

What is the condition that allows me to know which source my image is coming from?

Updated:

Now it's working and here is the solution:

    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
    {
        if(data.getData()!=null)
        {
            try 
            {
            if (bitmap != null) 
                {
                    bitmap.recycle();
                }

            InputStream stream = getContentResolver().openInputStream(data.getData());
            bitmap = BitmapFactory.decodeStream(stream);
            stream.close();
            imageView.setImageBitmap(bitmap);
            }

        catch (FileNotFoundException e) 
            {
                e.printStackTrace();
            }

        catch (IOException e) 
            {
                e.printStackTrace();
            }
        } 

        else 
        {
            bitmap=(Bitmap) data.getExtras().get("data"); 
            imageView.setImageBitmap(bitmap);
        }

        super.onActivityResult(requestCode, resultCode, data);
    }
}

Thank you for you help!

Answer

Siddharth Lele picture Siddharth Lele · May 6, 2013

Although the current piece of code is a neat way of presenting options to choose from, I found that it was severely difficult to manage. At least in my use case it was. I need to store images taken off the Camera to process further in the Aviary SDK (if the user so chooses).

To that end, I would like to propose a workaround.

This does not address your question per se. But offers an alternative considering you need to know where the Image is coming from (Camera / Gallery).

AlertDialog.Builder builder = new AlertDialog.Builder(StatusUpdate.this);
builder.setTitle("Choose Image Source");
builder.setItems(new CharSequence[] {"Gallery", "Camera"}, 
        new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which) {
        case 0:

            // GET IMAGE FROM THE GALLERY
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");

            Intent chooser = Intent.createChooser(intent, "Choose a Picture");
            startActivityForResult(chooser, ACTION_REQUEST_GALLERY);

            break;

        case 1:
            Intent getCameraImage = new Intent("android.media.action.IMAGE_CAPTURE");

            File cameraFolder;

            if (android.os.Environment.getExternalStorageState().equals
                    (android.os.Environment.MEDIA_MOUNTED))
                cameraFolder = new File(android.os.Environment.getExternalStorageDirectory(),
                        "some_directory_to_save_images/");
            else
                cameraFolder= StatusUpdate.this.getCacheDir();
            if(!cameraFolder.exists())
                cameraFolder.mkdirs();

            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
            String timeStamp = dateFormat.format(new Date());
            String imageFileName = "picture_" + timeStamp + ".jpg";

            File photo = new File(Environment.getExternalStorageDirectory(), 
                    "some_directory_to_save_images/" + imageFileName);
            getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
            initialURI = Uri.fromFile(photo);

            startActivityForResult(getCameraImage, ACTION_REQUEST_CAMERA);

            break;

        default:
            break;
        }
    }
});

builder.show();

This is the result (I still maintain that you code gives a much better selection set, but again, not the simplest thing in your use case or in mine either):

enter image description here

Now you can process the result based on the source of the selection:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK)    {

        switch (requestCode) {
        case ACTION_REQUEST_GALLERY:

            break;

        case ACTION_REQUEST_CAMERA:

            break;          
        }

    }
};

UPDATED:

Found it!! There is an answer here on SO that addresses exactly what you need. It is still a * workaround of sorts*. In the sense that it does not rely on different requestCodes. But works nonetheless.

Strange I missed it when I was stuck with this. :-(

NOTE: I am not posting the code here and am linking to it instead. All props go to the original author. :-)