-1

I am trying to show the base64 image in image view but it doesn't display. I am decoding and setiing image bitmap of the imageview. I am dynamically adding imageviews. Is the issue because of Iam adding images dynamiclly? Below is the code snippet:

LayoutInflater inflaterDocuments = (LayoutInflater)GetSystemService(Context.LayoutInflaterService);
View inflatedHeaderLayout = inflaterDocuments.Inflate(Resource.Layout.imageListItem, null, false);
ImageView imageView = inflatedHeaderLayout.FindViewById<ImageView>(Resource.Id.imageView);
byte[] decodedString = Base64.Decode(base64String, Base64Flags.Default);
Bitmap bitMap = BitmapFactory.DecodeByteArray(decodedString, 0, decodedString.Length);
imageView.SetImageBitmap(bitMap);
imageView.Invalidate();
_imageLayout.AddView(inflatedHeaderLayout);

Edit

Base64 string file Base64.txt

Arti
  • 2,993
  • 11
  • 68
  • 121

2 Answers2

0

This is method to get base64

public static String getBase64String(Bitmap image) {
    String encodeString = null;
    try {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
        byte[] byteArray = byteArrayOutputStream.toByteArray();
        encodeString = Base64.encodeToString(byteArray, Base64.DEFAULT);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return encodeString;
}

Glide use for show Image

String photoId = base64;
    Glide.with(getApplicationContext())
            .load(photoId)
            .apply(RequestOptions.circleCropTransform())
            .into(employeeImage);
Masum
  • 1,037
  • 15
  • 32
-1

Create method to convert Base64 to Bitmap

public Bitmap BaseStringToBitmap(string imageBase64)
{
    byte[] imageBytes = Convert.FromBase64String(imageBase64);
    return BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);
}

Than use it on ImageView object

imageView.SetImageBitmap(BaseStringToBitmap(base64String));

Sample code

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    SetContentView(Resource.Layout.activity_main);
    var iv = FindViewById<Android.Widget.ImageView>(Resource.Id.image1);
    iv.SetImageBitmap(BaseStringToBitmap());
}   
public Bitmap BaseStringToBitmap()
{
    var bse64 = "/9j/4ZT8RXhpZgAASUkqAAgAAAARAA4BAgAgAAAA2gAAAA8BAgAgAAAA...";
    byte[] imageBytes = Convert.FromBase64String(bse64);
    return BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);
}

Axml file

<ImageView
android:id="@+id/image1"
android:background="#89CFF0"    
android:layout_width="200dp" 
android:layout_height="200dp"/>

Output screenshot

enter image description here

R15
  • 13,982
  • 14
  • 97
  • 173
  • @Arti - Can you please provide `base64` string in text file or some other way, so that I can check it. – R15 Jan 30 '19 at 08:55
  • I can't upload base64 it gets stuck. – Arti Jan 30 '19 at 09:26
  • But the base64 opens up if I put it in browser. – Arti Jan 30 '19 at 09:27
  • You can make public URL of your `base64` using this site https://textuploader.com/ – R15 Jan 30 '19 at 09:30
  • No help with that, even textuploader gets stuck. – Arti Jan 30 '19 at 09:36
  • Oh! If you have gmail or outlook a/c than upload .txt file & share it as public, to get public url. From Google drive or OneDrive. – R15 Jan 30 '19 at 09:40
  • It worked. But now new issue. When I do it in a loop only 1st 3 images are displayed and then I am getting outOfMemory error. – Arti Jan 30 '19 at 10:58
  • @Arti - The size of Bitmap is fixed. If it is exceeding then it would throw exception. You need to resize it. For more information visit this https://stackoverflow.com/questions/11133645/imageview-outofmemoryexception – R15 Jan 30 '19 at 11:01