0

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;
                    textView15.setText(data);
                    if (object.has("profession")) {
                        String attr2 = object.getString("profession");
                        data2 = "" + attr2;
                        textView16.setText(data2);
                    }
                    if(object.has("company")){
                        String attr3 = object.getString("company");
                        data3 = "" + attr3;
                     textView38.setText(data3);
                    }

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

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

What if image size is bigger? It'll throw OutOfMemory exception.
1

If you want to use Picasso Image library for this, you can do it like this:

if (object.has("profile_image") {
  Picasso.with(context).load(object.get("profile_image")).into(R.id.image_view);
}

Check out https://github.com/square/picasso for documentation.

Comments

1

This can be done using Picasso

for that add dependancy in build.gradle file of your project

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'com.android.support:design:23.0.1'
    compile 'com.squareup.picasso:picasso:2.4.0'
}

and add code in your method as below

if(object.has("profile_image")) {
    // imageView should be declared in layout xml file with id `imageView`
    ImageView imageView = (ImageView) context.findViewById(R.id.imageView);
    com.squareup.picasso.Picasso.with(context).
        load(object.get("profile_image")).
        placeholder(R.mipmap.ic_launcher).
        into(imageView);
}

and done.

NOTE : object.get("profile_image") should return full path of image.

for e.g. http://www.example.com/images/image1.jpg

then only it will work.

2 Comments

ImageView circleView = (ImageView) context.findViewById(R.id.circleView); com.squareup.picasso.Picasso.with(context).load(object.get("profile_image")).placeholder(R.mipmap.ic_launcher).into(circleView);
I m getting error with these 2 lines for context. Even if i declare it i m getting error
0

If your image data in "profile_image" is Base64 encoded string then you can convert to bitmap by following.

if(object.has("profile_image")) 
{
     byte[] decodedString = Base64.decode(object.getString("profile_image", Base64.DEFAULT);
     Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.