Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Picture coming from camera or gallery?

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!

like image 402
Hossam Oukli Avatar asked May 06 '13 02:05

Hossam Oukli


People also ask

How do you get an image from a camera or a gallery and save it in Android?

Run the application on an Android phone. Selecting "Take photo" will open your camera. Finally, the image clicked will be displayed in the ImageView. Selecting "Choose from Gallery" will open your gallery (note that the image captured earlier has been added to the phone gallery).

What is the difference between picture and gallery?

Google Photos is accessible everywhere — mobile, desktop and web. It is available on Android, iOS, and has a web version. While a proper Windows or Mac app doesn't exist, you get a tool for uploading files. Gallery apps are exclusive to Android devices.

Why are my camera photos not showing in gallery?

If your photos are visible in My Files but are not in the Gallery app, these files may be set as hidden. This prevents Gallery and other apps from scanning for media. To solve this, you can change the option for showing hidden files.


2 Answers

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. :-)

like image 169
Siddharth Lele Avatar answered Oct 04 '22 17:10

Siddharth Lele


You can distinguish by using REQUEST_CODE

    private static final int PICK_FROM_CAMERA = 1;
        private static final int PICK_FROM_GALLARY = 2;

        /* For Image capture from camera */
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, PICK_FROM_CAMERA);   

    /* For Image capture from Gallary*/             
        startActivityForResult(new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI),
                                        PICK_FROM_GALLARY);
                        }



public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);

        switch (requestCode) {
               case PICK_FROM_CAMERA:
            if (resultCode == Activity.RESULT_OK) {
             Bitmap bitmapImage = (Bitmap) intent.getExtras().get("data");
              }
              break;

                case PICK_FROM_GALLARY:
               if (resultCode == Activity.RESULT_OK) {
              Uri mImageCaptureUri = intent.getData();
               }
             break;
         }
}
like image 3
Amit Gupta Avatar answered Oct 04 '22 15:10

Amit Gupta