皮卡索图片url在加载前的查找

3

目前,我正在调查使用Picasso库替换项目中大量自定义图片加载AsyncTask的可能性,似乎很有前途。然而,有一个问题我不确定如何使用Picasso解决。

在这种情况下,我们正在下载音乐曲目的专辑封面,但当我们要显示ListView时,我们只有曲目ID。因此,我们首先必须根据曲目ID查找专辑封面URL,然后将其加载到ImageView中。目前,我们有一个AsyncTask,在doInBackground()中首先查找图像URL,然后从中加载Bitmap,最后将其传递给onPostExecute。

是否有任何方法可以使用Picasso进行预查找,还是我们必须在AsyncTask中包装Picasso调用,以便首先执行查找(感觉这有点违背其目的)。

更新:现在的工作方式:

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
你需要位图用于其他目的吗?如果是这样,你可以直接从ImageView中获取它。 - Matter Cat
什么是预查找?您必须从本地存储中显示占位符,或者显示与服务器原始图像不同的预查找图像。 - Kirankumar Zinzuvadia
目前正在编辑问题,但简而言之,我们正在为曲目加载专辑封面,当我们的ListView要显示时,我们只有曲目ID。因此,我们首先需要根据曲目ID查找专辑封面URL,然后将该URL加载到ImageView中。 - patrick.elmquist
你能解决这个问题吗?我也遇到了同样的问题。 - Henrique Miranda
显示剩余4条评论
2个回答

1
我需要为我的应用程序解决同样的问题。我使用了OkHTTP拦截器来重定向请求。
这是我的Picasso声明:
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;
    }
}

这只是为我返回PNG数据,我看不到获取图像URL的方法? - Donal Rafferty

0
你需要在创建listView时使用Piccaso从你的专辑封面URL中加载图片。然后在曲目的详细信息屏幕上,你需要调用实际的图片URL来设置它在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);
                } 
            } 

是的,但这种解决方案的问题(如果我没有弄错的话)是getAlbumArtUriFromTrackId(id)调用将在UI线程上进行,这是不可能的,因为网络调用不允许在UI线程上进行。 - patrick.elmquist
我想要的是在Picasso加载图像的同时,在同一线程上进行URL查找。另一种选择是创建一个AsyncTask,然后启动Picasso.load(..),但我不认为这是一个好的解决方案。 - patrick.elmquist
Picasso调用移动到onPostExecute会导致在滚动ListView时图像闪烁/最终出现在错误的位置。这是我想通过改用Picasso解决的问题之一,因为它可以处理这种类型的问题(至少在正常加载图像时)。 - patrick.elmquist
你是将ImageView从构造函数传递给AsyncTask,还是在你的适配器类中有AsyncTask类? - Kirankumar Zinzuvadia

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接