Question

how to get image from gallery on click of a button in android? I am new to android, please help.

Was it helpful?

Solution

It is very easy check this code. good luck

public class MainActivity extends Activity {

Button btn1;
ImageView img;
private static int LOAD_IMAGE_RESULTS = 1;
Bitmap  mBitmap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn1 = (Button) findViewById(R.id.button1);
    edit = (EditText) findViewById(R.id.editText1);
    img = (ImageView) findViewById(R.id.ImageView1);

    img.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            // Start new activity with the LOAD_IMAGE_RESULTS to handle back the results when image is picked from the Image Gallery.
            startActivityForResult(i, LOAD_IMAGE_RESULTS);

        }
    });
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Here we need to check if the activity that was triggers was the Image Gallery.
    // If it is the requestCode will match the LOAD_IMAGE_RESULTS value.
    // If the resultCode is RESULT_OK and there is some data we know that an image was picked.
    if (requestCode == LOAD_IMAGE_RESULTS && resultCode == RESULT_OK && data != null) {
        // Let's read picked image data - its URI
        Uri pickedImage = data.getData();
        // Let's read picked image path using content resolver
        String[] filePath = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
        cursor.moveToFirst();
        imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));

        // Now we need to set the GUI ImageView data with data read from the picked file.
        mBitmap = BitmapFactory.decodeFile(imagePath);
        img.setImageBitmap(BitmapFactory.decodeFile(imagePath));

        // At the end remember to close the cursor or you will end with the RuntimeException!
        cursor.close();
    }
}

}

OTHER TIPS

Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);

And u will get picture path on u onActivityResult

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
            ImageView imageView = (ImageView) findViewById(R.id.imgView);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }
    }

and you use this to get full code HERE

use the following code it's work perfectly:

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ImageView imageView = (ImageView) findViewById(R.id.imgView);
    Button yourbutton = (Button) ( findViewById(R.id.button);

    yourbutton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, TAKE_PICTURE);

        }
    });
 }

outside yout oncreate:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);


    switch(requestCode){

        case TAKE_PICTURE:             
            if(resultCode==Activity.RESULT_OK) {

                // get bundle
                Bundle extras = data.getExtras();

                // get 
                takenPictureData = (Bitmap) extras.get("data");
            //  imageView.setImageBitmap(bitMap);
            }               
            break;  
    }

    //And show the result in the image view when take picture from camera.
    if(takenPictureData!=null){
        imageView.setImageBitmap(takenPictureData);
    }       
}

Best of luck :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top