rahulxxarora rahulxxarora - 2 months ago 17
Android Question

How to remove lagging in RecyclerView showing captured images from the camera?

I am showing images captured from camera on cards using RecyclerView. I don't know why the app lags whenever I scroll down the screen. I am also using Picasso to load images async. Please help. Below is the code :

Check the onBindViewHolder function

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class DiaryAdapter extends RecyclerView.Adapter<DiaryAdapter.ViewHolder>{

List <DiaryInfo> mItems;

public DiaryAdapter() {
super();
mItems = new ArrayList <DiaryInfo> ();
mItems = MainActivity.diaryinfo;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.content_main, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
DiaryInfo info = mItems.get(i);
viewHolder.date.setText(info.getDate());
viewHolder.desc.setText(Html.fromHtml((info.getDesc()).replaceAll("''","\"")));

String data = info.getThumbnail();

String path=""; // Final path of the thumbnail

final SQLiteDatabase mydatabase = (MainActivity.mContext).openOrCreateDatabase("remember", android.content.Context.MODE_PRIVATE, null);

// While changing database information here change at Itemadd's done button listener also!
mydatabase.execSQL("CREATE TABLE IF NOT EXISTS memories_pics(id VARCHAR PRIMARY KEY, path VARCHAR)");

Cursor cursor = mydatabase.rawQuery("SELECT * FROM memories_pics WHERE id=\'" + data + "\' LIMIT 1", null);
if (cursor.moveToFirst()) {
while (cursor.isAfterLast() == false) {
path = cursor.getString(cursor
.getColumnIndex("path"));
cursor.moveToNext();
break;
}
}
cursor.close();

//Bitmap bmp = BitmapFactory.decodeFile(String.valueOf(path.split("=")[0]));
//viewHolder.imgThumbnail.setImageURI(Uri.parse(String.valueOf(path.split("=")[0])));
File file = new File(String.valueOf(path.split("=")[0]));
Picasso.with(MainActivity.mContext).load(file).into(viewHolder.imgThumbnail);
}

@Override
public int getItemCount() {
return mItems.size();
}

public Object getItem(int pos) { return mItems.get(pos); }

class ViewHolder extends RecyclerView.ViewHolder {

public ImageView imgThumbnail;
public TextView date, desc;

public ViewHolder(View itemView) {
super(itemView);
imgThumbnail = (ImageView) itemView.findViewById(R.id.thumbnail);
date = (TextView)itemView.findViewById(R.id.date);
desc = (TextView)itemView.findViewById(R.id.description);
}
}


}

Answer

Try using

Picasso.with(MainActivity.mContext).load(file).fit().centerCrop().into(viewHolder.imgThumbnail);
Comments