Search Image Picasso Image Before Uploading

So, I am currently looking into replacing a lot of custom image loading AsyncTask

in a project with the Picasso library, and that seems promising. However, there is one problem: I'm not really sure how to solve the problem using Picasso.

In this case, we are loading Album Art for the music tracks, however, when our ListView is shown, we only have the track ids. So first we have to search for the album url based on the track id and then load it into the ImageView. We currently have AsyncTask

where we at doInBackground()

first look at the image url and then load from it Bitmap

, which is then pasted into onPostExecute

.

Is there a way to do this preliminary search with Picasso or will we have to wrap the Picasso call in AsyncTask

, which will do the search first (and feels like it kind of defeats the target).

Update: how it works now:

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

      

Update 2: An example is given of what I am after

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

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

     })

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

      

+3


source to share


2 answers


I had to solve the same problem for my application. I used the OkHTTP interceptor to redirect the request.

This is my picas ad:

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

      



This is the basic implementation of the interceptor:

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

      

+1


source


You have to load images from your album url using Piccaso while your listView is created. Then on the Track Detail Screen, you have to call your actual image url to set it in image view mode.

Edited



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


source







All Articles