Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swapping Image in ImageView

Tags:

android

image

I've been trying to implement simple (I think) thing in my app. I thought it would be simple, but somehow it doesn't work as I would wanted it to.

What I'm trying to do is:

  • There is an activity with ImageView in center - it has it's own image (well, there's two of them).
  • I want to press that ImageView (or it could be ImageButton), open the gallery/take picture with camera.
  • Then, after taking/picking a picture, I want to make that ImageView display the thumbnail of picture.

My layout

                <ImageView
                    android:id="@+id/add_photo_left"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="20dp"
                    android:layout_weight="1"
                    android:adjustViewBounds="true"
                    android:src="@drawable/add_photo_button" />

                <ImageView
                    android:id="@+id/add_photo_right"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:adjustViewBounds="true"
                    android:src="@drawable/add_photo_button" />
            </LinearLayout>

My code - after getting picture path

        BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
        bmpFactoryOptions.inSampleSize = 8;
        Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/5.png",bmpFactoryOptions);
        lastClicked.setImageBitmap(bitmap);
        //lastClicked.setImageResource(R.drawable.green_circle);

What I wanted to do is to read the resized file (using inSampleSize) from sdcard, and then fill ImageView (or ImageButton) with it. But after using setImageBitmap function, the view disappears - like it just loaded empty image (commented line works properly - it's just green dot).

Maybe someone has approached similar problem? I would much appreciate any kind of help.

Greetz, scana

EDIT: ofc lastClicked:

lastClicked = (ImageView)findViewById(R.id.add_photo_left);
like image 861
scana Avatar asked Oct 08 '22 19:10

scana


1 Answers

Why don't you try lastClicked.invalidate() ?

I do so in my code at least and it works this way ;)

like image 64
mjaskowski Avatar answered Oct 12 '22 10:10

mjaskowski