I Want to load image from json in my assest as a string and then pass it to an imageview. How to do it?
CodePudding user response:
Say your json is something like:
const book = {
id: 1,
title: "my title",
img:"/image/file.png",
...
}
...
you may pass that object directly in your image tag:
eg.
<img src="{book.img}" />
CodePudding user response:
You can use Library like Glide and Picasso for load image in imageview as a string Please check below link How to load Image into ImageView from Url using Glide v4.0.0RC1
CodePudding user response:
If you want to store the whole image in your JSON, than you could use base64 to convert from byte[] to String and vice versa. Java has base64 encoders and decoders in it's util packages. Wiki page: https://en.wikipedia.org/wiki/Base64.
CodePudding user response:
Get image as byteArray from JSON, then set it into imageView.
//byte[] imageData
ImageView imgViewer = (ImageView) findViewById(R.id.image);
Bitmap bm = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
imgViewer.setImageBitmap(bm);