2013-01-06 19 views
1

現在我正在使用下面的一段代碼檢索圖像,它工作的很好。但我想用universal-image-loader實現緩存。我已經在我的其他項目中實現了它,其中我完全像url圖像一樣。 \ pic1.jpeg。另一方面,在使用Contacts api v3時,我必須處理輸入流,並且我沒有這樣完整的url.so,我不知道如何執行universal-image-loader是否有可能借助通用圖像加載程序檢索使用Contacts api v3的聯繫人圖像?

爲參考:Contact api v3

這裏是我使用的是現在的代碼:

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); 
    } 

回答

1

是,universal-image-loader可以讓你做到這一點。只要按照這個步驟:

  1. 您可以介紹一下你自己的URL類型,例如contacts-api-v3://user_id=<user_id>
  2. 提供一種方法來檢索InputStream這樣的URL:

    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. 配置ImageLoader使用您的CustomImageDownloader

    final ImageLoaderConfiguration.Builder builder = new ImageLoaderConfiguration.Builder(context); 
    
    // some basic configuration should be here 
    
    builder.imageDownloader(new CustomImageDownloader()); 
    
  4. 現在你可以使用這種方式:

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

如果ContactPhotoLink是一樣的東西'''https://www.google.com/m8/feeds/contacts/ {} USEREMAIL/... '''然後我建議用** contacts **替換** https **,將它傳遞給imageLoader:'''imageLoader.displayImage(「constacts://www.google.com/m8/feeds/contacts/ .. 。「),然後在CustomImageDownloader中檢查imageUri.getScheme()。equals(」contacts「)''',如果是這樣,用** https **替換** **的**接觸**並通過此方法檢索InputStream鏈接。 – NOSTRA

相關問題