Amren
Amren

Reputation: 25

Unable to select image from gallery android app (grayed out)

I'm trying to allow the app user to select an image from their gallery and only set the image uri if the user actually selects it. The problem I'm facing is that when I open the gallery of images all of the images are grayed out and I cannot select an image regardless of the image format. Here is my code for that section:

    hostImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        Intent imageIntent = new Intent();                                                      
        imageIntent.setType("image*/");                                                         
        imageIntent.setAction(Intent.ACTION_GET_CONTENT);                                       
        startActivityForResult(Intent.createChooser(imageIntent,"Choose host image"),000);      

        }
       }
    );
}

public void onActivityResult(int askCode, int retCode, Intent info){                             
    if (retCode==RESULT_OK){
        if (askCode==000){
            hostImageView.setImageURI(info.getData());                                      
        }     
}

Upvotes: 2

Views: 5228

Answers (3)

Keerthana Nerella
Keerthana Nerella

Reputation: 11

The asterisk symbol(*) should be given after forward slash(/).

Content-type "image/*" means category image , with any extensions (.jpeg, .png , ...)

The following worked for me

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);

Upvotes: 0

Sujith Thankachan
Sujith Thankachan

Reputation: 56

Update your code for gallery opening as follows:

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

And use the number 2(Or the number you are using for starting activity) inside onActivityResult method

Upvotes: 3

Heshan Sandeepa
Heshan Sandeepa

Reputation: 3688

I suggest you to get the selected image path instead of imagetype, and then load it into your image view. Try this,

    public boolean  click(View v)
{
    Intent intent = new   Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intent, 1);
    return true;
}

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

    if (requestCode == 1 && 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.imageView1);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

    }
}

Upvotes: 0

Related Questions