Android code to retrieve the server image and view it in Imageview


Hi I know how to fetch an string from jsonobject, but my question is how to fetch an image from Rest api and display it. The image is stored as profile_image in jsonobject

My code:

 try {
                    JSONObject jsonObject = new JSONObject(response);
                    JSONObject object = jsonObject.getJSONObject("user");
                    String attr1 = object.getString("username");
                    data = "" + attr1;
                    if (object.has("profession")) {
                        String attr2 = object.getString("profession");
                        data2 = "" + attr2;
                        String attr3 = object.getString("company");
                        data3 = "" + attr3;

                    if(object.has("profile_image")) {
                        //what has to be done here

There are several possible cases:

Case 1. Image is Base64, then you need to decode it and use BitmapFactory method:

byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

Case 2. Json contains link to image. Simply load the link, and when you receive Stream object, pass it to BitmapFactory. Something like this:

InputStream instream = httpEntity.getContent();
bmp = BitmapFactory.decodeStream(instream);

Above example uses HttpClient class, search api docs on how to get InputStream from your used network library.