Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Cannot set gallery image to ImageView

I've looked through tons of posts and cannot figure out why I can't get this to work. All I want to do is have the user click a button that opens up the gallery app. Then the user selects a picture which automatically closes out the gallery and goes back to my application where it automatically sets that image to an ImageView.

So far, I have it working all the way up until it goes back to my application. It seems to all be fine but the image never shows up in the ImageView.

Here is the XML code for the ImageView:

<ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="14dp"
        android:layout_gravity="center_horizontal" />

At the beginning of my activity I set the ImageView with this:

 ImageView targetImage;

And here is the rest of my code to get the image and set it to my ImageView. There is a button that launches "setGunImage".

public void setGunImage(View view) {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
}

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

if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            targetImage = (ImageView)findViewById(R.id.imageView1);
            Uri selectedImageUri = data.getData();
            targetImage.setImageURI(selectedImageUri);
        }
    }
}

I have tested it on both the simulator with the sd card enabled and an image loaded into and also on a real device. Both give the same behavior. It goes through the gallery steps fine but when it goes back to my application there is no image loaded in the ImageView.

I tried changing the data to a bitmap and setting that but it never showed up either. I know it's probably something super simple that I'm just not seeing so hopefully a fresh pair of eyes can point me in the right direction. Thanks.

like image 838
dcabal Avatar asked Jul 29 '26 05:07

dcabal


1 Answers

I think Imran solution should work fine .............. and you can also try this way

 @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            InputStream stream = null;
         if( resultCode==RESULT_OK)
           {
              if(requestCode==SELECT_PICTURE)
               {
                try {
                    // We need to recyle unused bitmaps
                    if (bitmap != null) {
                        bitmap.recycle();
                    }
                    stream = getContentResolver().openInputStream(data.getData());
                    bitmap = BitmapFactory.decodeStream(stream);

                                targetImage = (ImageView)findViewById(R.id.imageView1);
                    targetImage.setImageBitmap(bitmap);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } finally {
                    if (stream != null)
                        try {
                            stream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                }
            super.onActivityResult(requestCode, resultCode, data);
        }

}

from link

like image 180
Dheeresh Singh Avatar answered Jul 31 '26 18:07

Dheeresh Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!