2015-05-13 63 views
3

所以我目前正在研究在畢加索圖書館裏用畢加索圖書館替代大量自定義圖片加載AsyncTask的可能性,它看起來很有希望。然而,有一個問題我不完全確定如何使用畢加索解決。加載之前的畢加索圖片url查找

在這種情況下,我們正在爲音樂曲目下載專輯封面,但是當我們的ListView要顯示時,我們只有曲目ID。首先,我們必須根據曲目ID查找專輯封面的URL,然後將其加載到ImageView中。目前我們有一個AsyncTask,我們在doInBackground()首先查找圖像的URL,然後加載一個Bitmap,然後粘貼到onPostExecute

是否有任何方法使用畢加索進行預先查找,或者我們必須將首次執行查找的中的畢加索調用包裝起來(並且感覺它有點違背目的)。

更新:它是如何工作現在:

private class AlbumArtTask extends AsyncTask<String, Bitmap, Bitmap> { 

    @Override 
    protected Bitmap doInBackground(String... strings) { 
     final String id = strings[0]; 

     // This part is what I'm asking for, basically to be able to make an Async 
     // lookup of the url that will later be used by Picasso 
     final Uri uri = getAlbumArtUriFromTrackId(id); 

     // Creating the Bitmap and return it to be used in an ImageView 
     // This part is handled by Picasso 
     return bitmap = createBitmapFromUri(uri); 
    } 

    @Override 
    protected void onPostExecute(Bitmap bitmap) { 
     // load image into ImageView 
    } 
} 

更新2:由例如我後

Picasso.with(mContext) 
    .load(new UriProvider() {   

     // Get the actual image URI here 
     public Uri getImageUri() { 
      return getAlbumArtUriFromTrackId(id); 
     } 

    }) 

     // And load it here 
    .into(mImageView); 
+1

什麼是查找裝貨前的目的是什麼? – fida1989

+0

是否因爲您需要位圖用於其他目的?如果是這種情況,可以直接從imageview中獲取。 –

+0

預查找是什麼意思?您必須從本地存儲器顯示佔位符,或者必須顯示其他原始服務器的預查找圖像。 –

回答

0

你必須從你的相冊載入圖片藝術網址在創建listView時使用Piccaso。然後在軌道的詳細信息屏幕上,您必須調用您的實際圖片網址以在imageview中設置它。

編輯

private class AlbumArtTask extends AsyncTask<String, Bitmap, Uri> { 

     ImageView mImageView; 
     Context mContext;  
     public AlbumArtTask(Context context,ImageView imageView){ 
      this.mImageView=imageView; 
      this.mContext=context; 

     } 

       @Override 
       protected Uri doInBackground(String... strings) { 
        final String id = strings[0]; 

        // This part is what I'm asking for, basically to be able to make an Async 
        // lookup of the url that will later be used by Picasso 
        final Uri uri = getAlbumArtUriFromTrackId(id); 

        // Creating the Bitmap and return it to be used in an ImageView 
        // This part is handled by Picasso 
        return uri; 
       } 

       @Override 
       protected void onPostExecute(Uri uri) { 

        // You have to pass imageview in constructor. 
        // load image into ImageView 
      Picasso.with(mContext).load(uri)/* And load it here*/.into(mImageView); 
       } 
      } 
+0

是的,但該解決方案的問題(如果我沒有弄錯)是'getAlbumArtUriFromTrackId(id)'調用將在UI線程上進行,這是不可能的,因爲UI線程上不允許網絡調用 –

+0

什麼我之後是在Picasso加載圖像時在同一個線程上進行URL查找。另一種方法是創建一個AsyncTask,然後啓動Picasso.load(..),但我不認爲這是一個很好的解決方案。 –

+0

檢查編輯答案。 –

1

我不得不解決同樣的問題,我的應用程序。我使用OkHTTP攔截器重定向請求。

這是我的Picassa中聲明:

OkHttpClient client = new OkHttpClient(); 
client.interceptors().add(new PhotoInterceptor()); 
Picasso picasso = new Picasso.Builder(context) 
    .downloader(new OkHttpDownloader(client)) 
    .build(); 

這是一個基本的實施攔截:

public class PhotoInterceptor implements Interceptor { 

    Gson gson = new Gson(); 

    @Override 
    public Response intercept(Chain chain) throws IOException { 
     Request request = chain.request(); 
     Response response = chain.proceed(request); 
     if (response.isSuccessful()) { 
      Photo photo = gson.fromJson(response.body().charStream(), Photo.class); 
      if (photo!=null) { 
       request = request.newBuilder() 
        .url(photo.getPhoto_file_url()) 
        .build(); 
       response = chain.proceed(request); 
      } 
     } 

     return response; 
    } 
} 
+0

這只是爲我返回的png數據,我看不到一種方式來獲取圖像的網址? –