2015-05-13 62 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数据,我看不到一种方式来获取图像的网址? –