Selecting ImageFrom Gallery

Hi ,
  In this post i am going to tell you about on how to pick a image from gallery and use that image in your application.

Step1:
Intent photo_pick=new Intent(Intent.ACTION_PICK);
                photo_pick.setType("image/*");
                photo_pick.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(photo_pick,
                        "Select Picture"), 1);


Step2:
As you have used the startActivityForResult so now you should check for the result using the onActivtyResult method as follows:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1){
            /*** To get the path and image ***/           
            if(resultCode == RESULT_OK){ 
                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 filePath = cursor.getString(columnIndex);
                Log.e("","filepath"+filePath);// file path of selected image
                cursor.close();
                Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
                _show_image.setImageBitmap(yourSelectedImage); //set your bitmap in your imageView
            }

}


Source Code: https://sites.google.com/site/quickandroidblog/download-stuff

Comments

Popular Posts