Samuel de Beer
Samuel de Beer

Reputation: 21

Displaying a Bitmap image (ImageView)

first time asker here. I am brand new to Android Studio and I'm busy creating an App in it that will scan a QR code that contains a Base64 encoded image (in the form of a Base64 String). This string will then need to be decoded and the resulting image must be displayed. I think I've managed to do the decoding correctly using:

 byte[] imageBytes = Base64.decode(result.getContents(), Base64.DEFAULT);
 Bitmap decodedByte = BitmapFactory.decodeByteArray(imageBytes, 0,imageBytes.length);        

Where " result.getContents() " returns the Base64 String that will be converted.

The part that's tripping me up is then displaying this using ImageView and XML. I currently have this:

ImageView image = (ImageView) findViewById(R.id.image);
image.setImageBitmap(decodedByte);    

I am a bit confused by how I am supposed to display this image using XML, as I understand that 'image' in "findViewById(R.id.image);" is the XML image ID. All the examples I've seen have involved displaying drawable, static images but I'm wanting to display an image that would change depending on which Base64 String I'm decoding. I have just started learning XML and I understand that a usual ImageView code block for a drawable picture would look something like this:

<ImageView 
android:id="@+id/x2"
android:src="@drawable/book"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />    

My question is how would I change this in order to display the image I have decoded.

Upvotes: 1

Views: 5497

Answers (3)

rahadi
rahadi

Reputation: 156

To set the image's of the ImageView dynamically is by doing it programmatically which means that it's done not by the XML, but by the Java code.

So in your case, the XML part is used to arrange your contents, including the <ImageView>. To access it to your Activity class, you give this <ImageView> an id. (in your case it is done by setting the android:id=@+id/x2 attribute. You guessed it, so your <ImageView>'s id is x2.

To set the image of this <ImageView>, you need to access it first. That's why you need findViewById(). So, to set the image, access it by

ImageView image = (ImageView) findViewById(R.id.x2);

Now you have it, then just set the image

image.setImageBitmap(decodedByte);

Upvotes: 1

sandy
sandy

Reputation: 85

The XML when loaded basically creates java view objects so your imageview is basically a container for your images. When you decode the the string into a bitmap and set the bitmap to the imageview, you are telling your JVM to reflect any changes done to your images to be reflected in the imageview. SO you dont need to add any code to the XML to do that.

Upvotes: 0

CodeChimp
CodeChimp

Reputation: 4835

Your ImageView within XML has a different ID to what you are trying to set. Change your findViewById to x2 as below

ImageView image = (ImageView) findViewById(R.id.x2);
image.setImageBitmap(decodedByte);    

Upvotes: 4

Related Questions