Skip to content Skip to sidebar Skip to footer

Android Intent - Pick Image From Gallery

I am following this sample code to send an intent to get an image. Intent i = new Intent(); i.setAction(Intent.ACTION_GET_CONTENT); i.setType('image/*'); if (i.resolveActivity(g

Solution 1:

Can you try his answer? How to pick an image from gallery (SD Card) for my app?

use getData() instead of getParcelableExtra()

It will return you an Uri and you will need to use the ContentResolver to get the Image.

Also, use Intent.ACTION_PICK instead of Intent.ACTION_GET_CONTENT.

Solution 2:

Instead of getting the bitmap, fetch the URI from data and load the bitmap yourself.

UriselectedImageUri= data.getData();
           StringselectedImagePath= getPath(selectedImageUri);

Solution 3:

Try this.. It help you

/**
                     * Images uploaded through gallery
                     * 
                     */

                    btn_gallery.setOnClickListener(newOnClickListener()
                    {

                        @OverridepublicvoidonClick(View v)
                        {
                            if (v.getId() == R.id.btn_gallery)
                            {
                                upload = true;
                                gallery_upload();


                            }

                        }
                    });



     /**
             * By this method we can upload the image from gallery
             */publicvoidgallery_upload()
            {
                Log.d("Upload", "inside gallery upload");
                Intenti=newIntent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(i, RESULT_LOAD_IMAGE);

            }

    @OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data)
        {
            super.onActivityResult(requestCode, resultCode, data);

            if (upload)
            {
                if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)
                {
                    UriselectedImage= data.getData();
                    String[] filePathColumn = { MediaStore.Images.Media.DATA };

                    Cursorcursor= getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                    cursor.moveToFirst();

                    intcolumnIndex= cursor.getColumnIndex(filePathColumn[0]);
                    picturePath = cursor.getString(columnIndex);
                    Log.e("", "" + picturePath);
                    cursor.close();

                    imageView = (ImageView) findViewById(R.id.upload_register_image);

                    Fileimage=newFile(picturePath);
                    if (image.exists())
                    {
                        Filef= image.getAbsoluteFile();
                        decodeFile(f);
                    }
                    else
                    {
                        Log.d("VEHICLEREGISTERATION", "image doesnt exist");
                    }

                    upload = false;

                    imagePath(picturePath);

                }
    }
    }
/**
 * Helps to decode size of the image
 * */private Bitmap decodeFile(File f)
{
    try
    {
        // Decode image size
        BitmapFactory.Optionso=newBitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(newFileInputStream(f), null, o);

        // The new size we want to scale tofinalintREQUIRED_SIZE=70;

        // Find the correct scale value. It should be the power of 2.intscale=1;
        while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
            scale *= 2;

        // Decode with inSampleSize
        BitmapFactory.Optionso2=newBitmapFactory.Options();
        o2.inSampleSize = scale;
        Bitmapbit_map= BitmapFactory.decodeStream(newFileInputStream(f), null, o2);
        imageView.setImageBitmap(bit_map);

    }
    catch (FileNotFoundException e)
    {
    }
    returnnull;
}

publicvoidimagePath(String Path)
{
    finalImgPath = Path;

}

Post a Comment for "Android Intent - Pick Image From Gallery"