Search code examples
androidgdata-apiuniversal-image-loader

is it possible to retrieve image of contact using Contacts api v3 with the help of universal-image-loader?


right now I am retrieving images using following piece of code and it is working perfectly.but I want to implement cache using universal-image-loader.I have already implemented it in my other projects in which I was having complete url of image like ~\images\pic1.jpeg .on the other hand,while using Contacts api v3 I have to deal with input streams and I don't have such complete url.so I don't know how to implement universal-image-loader.

for the reference:Contact api v3

here is the code which I am using right now:

    Bitmap bm=BitmapFactory.decodeResource(HomeActivity.this.getResources(),
            R.drawable.profile_pic);
    CONSTANTS.buffer = new byte[4096];

// iterate the loop upto number of contacts
    for(int i=0;i<CONSTANTS.contactArrayList.size();i++)
    {


//if the contact has any profile pic then retrieve it otherwise set default profile pic from drawable folder

    if(CONSTANTS.contactArrayList.get(i).getContactPhotoLink().getEtag()!=null)
        {
            try
            {
                GDataRequest request = CONSTANTS.mContactService.createLinkQueryRequest(CONSTANTS.contactArrayList.get(i).getContactPhotoLink());
                  request.execute();
                  InputStream in = request.getResponseStream();
                  CONSTANTS.buffer = ByteStreams.toByteArray(in);
                  bm = BitmapFactory.decodeByteArray(CONSTANTS.buffer, 0, CONSTANTS.buffer.length);
                  in.close();
                  request.end();
            }
            catch (Exception e) {
                UTILS.Log_e("loadProfilePics error", e.toString());
            }

        }
        else
        {
            bm = BitmapFactory.decodeResource(HomeActivity.this.getResources(),
                    R.drawable.profile_pic);
        }
         CONSTANTS.contactArrayList.get(i).setContactPhoto(bm);
     }

Solution

  • Yes, universal-image-loader allows you to do it. Just follow this steps:

    1. You can introduce your own url type, for example contacts-api-v3://user_id=<user_id>
    2. Provide a way to retrieve InputStream for such urls:

      public class CustomImageDownloader extends URLConnectionImageDownloader {
          @Override
          protected InputStream getStreamFromOtherSource(URI imageUri) throws IOException {
              if (imageUri.getScheme().equals("contacts-api-v3")) {
                  // here you can use code provided in your question
                  return retriveInputStreamForThisUser();
              }
              return null;
          }
      }
      
    3. Configure ImageLoader to use your CustomImageDownloader:

      final ImageLoaderConfiguration.Builder builder = new ImageLoaderConfiguration.Builder(context);
      
      // some basic configuration should be here
      
      builder.imageDownloader(new CustomImageDownloader());
      
    4. Now you can use it this way:

      ImageLoader.getInstance().displayImage("contacts-api-v3://user_id=123", imageView);