Skip to content Skip to sidebar Skip to footer

Let User Select An Image From The Drawable Folder

I want to select image from my android studio drawable folder, which now I have 2 images in it named example.jpg and sue.PNG. and I define an array as int image[] = {R.drawable.exa

Solution 1:

Try this . On your button click

open Intent

publicvoidimageChooser() {
    // Gallery Intent
    Intent galleryIntent = new Intent();
    galleryIntent.setType("image/*");
    galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(galleryIntent , "Select Picture"), 200);
}

Handle the image result

publicvoidonActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
  
    if (resultCode == RESULT_OK) {
        if (requestCode == 200) {
            // Get the uri UriselectedImageUri= data.getData();
            if (null != selectedImageUri) {
                // update your imageview 
                yourimageview.setImageURI(selectedImageUri);
            }
        }
    }
}

Post a Comment for "Let User Select An Image From The Drawable Folder"