Nirel
Nirel

Reputation: 1925

Load bitmap with picasso into ImageView

i'm building a file explorer application, and i'm trying to load large amount of images (more then 5000...), i'm trying to use picasso but its still slow so i'm trying to load a Thumbimage with picasso and not the whole image file..

    ....
    ImageItem item = data.get(position);
    ImageView imageView = holder.image;
    Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(item.getImage().getPath()), 100, 100);
    Picasso.with (context) .load (ThumbImage ) .into (imageView); /*/ doesnt work.. cant load ThumbImage with Picasso
    Picasso.with (context) .load (item.getImage()) .into (imageView); //work, but too slow

i cant figure out how can i load Thumbimage into ImageView with Picasso.. there is any why to do that? another way to load faster my images?

Upvotes: 0

Views: 2742

Answers (1)

Bryan
Bryan

Reputation: 15135

First I would recommend using RecyclerView instead of a GridView, considering it handles a lot of the memory management for you. Instead of having to load all of the images at once (or manage it yourself), it will only load the images that fit on the screen. And you can use a GridLayoutManager to have it appear just as it would in a GridView.

RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler);
recyclerView.setLayoutManager(new GridLayoutManager(context, 4);
recyclerView.setAdapter(new MyRecyclerViewAdapter(context, data);

Also, let Picasso handle converting the image for you, this way you do not need to use ThumbnailUtils. Picasso handles image resizing and memory management with the resize() function:

String uri = data.get(position);

Picasso.with(context).load(new File(uri)).resize(100, 100)
  .centerCrop().into(imageView)

You can do this in the onBindViewHolder() method of the RecyclerView.Adapter so that each image will only be loaded when the View has to be shown.

I am not sure how you are obtaining the images currently, but if you do not know how to obtain the URI of the image, this should work:

String[] projection = {"*"};

List<String> data = new ArrayList<>();

Cursor cursor = context.getContentResolver().query(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);

if(cursor.moveToFirst()) {
    do {
        data.add(cursor.getString(cursor
                        .getColumnIndex(MediaStore.Images.Media.DATA)));
    } while(cursor.moveToNext());
}

I do not know if this will fix the problem you have with Picasso not loading the images, but if it does not post a stack trace and we can try to figure that out.

Upvotes: 2

Related Questions