Spartons
Spartons

Reputation: 85

How to Load Images From gallery by clicking the imageview Android

I have a Image view in my activity...Where I want to place a Text view("Click To load Images") on the Image View..When user Click the Empty Image View the gallery section want to be called from there he can select the picture and it want to be load on Imageview

How can we Do that??

Upvotes: 2

Views: 2128

Answers (2)

Bhavin Kevadiya
Bhavin Kevadiya

Reputation: 224

public class ImageGalleryActivity extends Activity {
    ImageView imageView;
    private static int RESULT_LOAD_IMAGE = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        imageView = (ImageView) findViewById(R.id.ImageViewId);
        TextView LoadImage = (TextView) findViewById(R.id.TextViewId);
        LoadImage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent loadIntent = new Intent(Intent.ACTION_PICK,  
                 android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(loadIntent, RESULT_LOAD_IMAGE);
            }
        });
    }


    @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.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }
    }
}

Upvotes: 2

Attiq ur Rehman
Attiq ur Rehman

Reputation: 475

Try this code ....

AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Pick Image from")
            .setPositiveButton("Camera", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //camera intent
                    Intent cameraIntent = new Intent(ConversationActivity.this, CameraActivity.class);
                    cameraIntent.putExtra("EXTRA_CONTACT_JID", contact.getJid());
                    startActivity(cameraIntent);
                }
            })
            .setNegativeButton("Gallery", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    Intent intent = new Intent();
                    // Show only images, no videos or anything else
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    // Always show the chooser (if there are multiple options available)
                    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
                }
            });
    AlertDialog alert = builder.create();
    alert.show();

Upvotes: 3

Related Questions