M_rey3000
M_rey3000

Reputation: 204

How to Load many images in Recycle View without Problem in android

im using Glide library to load my images from storage to imageView using RecyclerView

all thing good

but when number of images be alot app crashed or should be wait so much to activity load completely this is my code

    public static File DIR_IMG = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "MyHairSamples");

    private void readExistingImages(){
    File[] images;
    try {
        images= DIR_IMG.listFiles();
        for (int i = 0; i < images.length; i++) {
            if (images[i].isFile() && images[i].length() > 50L) {
                Bitmap b = BitmapFactory.decodeFile(images[i].getAbsolutePath());
                addImage(b, "5", images[i].getName());
            }
            else{
                images[i].delete();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

and i using this method in onCreate method

and this is my Adapter Code

@Override
public void onBindViewHolder(@NonNull MyViewHolder viewHolder, int i) {
    viewHolder.title.setText(arrayList.get(i).getImageTitle());
    viewHolder.thumbnail.setLabelFor(i);
    Glide.with(context)
            .load(arrayList.get(i).getImage())
            .into(viewHolder.thumbnail);
}

Upvotes: 0

Views: 599

Answers (2)

Amine
Amine

Reputation: 2434

When the images resolution is big, loading will be slow and even your application will crash.

So you can resize your image set like this

  Glide.with(context)
        .load(arrayList.get(i).getImage())
        .apply(new RequestOptions().override(400, 400))
        .into(viewHolder.thumbnail);

EDIT

  RequestOptions reqOpt = RequestOptions
                        .fitCenterTransform()
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .override(300,300)

Upvotes: 1

Bhavesh Shiyani
Bhavesh Shiyani

Reputation: 98

Load images inside the uithread, it is not interrupt the main thread.

Use below code.

    context.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Glide.with(context)
        .load(arrayList.get(i).getImage())
        .into(viewHolder.thumbnail);
            }
        });

Upvotes: 1

Related Questions