ClassA
ClassA

Reputation: 2660

Android - SimpleCursorAdapter slow image loading

I'm getting images from my SQLite database that was stored as a BLOB and setting the Cursor to the Adapter with a SimpleCursorAdapter like this:

public class FragmentAdapter3 extends Fragment {
ArrayList<Sudentdb> list;
GridView gridView;

final String[] from = new String[]{SQLiteHelper._ID,
        SQLiteHelper.NAME, SQLiteHelper.AGE, "imagepath"};

final int[] to = new int[]{R.id.rowid, R.id.txtName, R.id.txtAge, R.id.img};

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_main_adapter3, null);
    gridView = (GridView) v.findViewById(R.id.gridView);
    empty = (TextView) v.findViewById(R.id.esc);

    final DBManager dbManager = new DBManager(getActivity());
    dbManager.open();

    final Cursor cursor = dbManager.fetch();

    list = new ArrayList<>();

    gridView.setAdapter(new SimpleCursorAdapter(getActivity(), R.layout.single_item, cursor, from, to, 0));     

    return v;
}

This works fine and I'm getting all the text and images back from SQLite, the problem is that the images gets loaded very slowly and this causes my application to 'lag' when switching between activities.

My question is, how can I load images from my SQLite database with a SimpleCursorAdapter without causing lag/loading issues when switching between activities?


EDIT:

I know I can use libraries like Piccaso, UniversalImageLoader or Volley that can handle the cache of the images for me, but I don't know how I would implement it with a String[] like above.


EDIT 2:

I'm now saving and getting the path of the images like the comments suggested, but the same problem occurs. The only thing happening in the log is:

Skipped 43 frames! The application may be doing too much work on its main thread.

This indicates to me that it is the loading of the images that is causing this issue, even after changing saving BLOB to saving file path.

Upvotes: 0

Views: 195

Answers (1)

user8562054
user8562054

Reputation:

Try Picasso and Glide library and in database store image as vachar as string path and call android side using Bitmap so your application will not "lag" when switch between activities

Picasso

Picasso.with(mContext).load(imageFile).priority(Picasso.Priority.HIGH).resize(width, height).into(image);

Glide

Glide.with(mContext).load(imageFile).override(width, height).into(image);

you can convert a image to PNG or JPEG like this:

try {
   FileOutputStream out = new FileOutputStream(filename);
   bmp.compress(Bitmap.CompressFormat.PNG, 100, out); //100-best quality
   out.close();
} catch (Exception e) {
   e.printStackTrace();
}

Upvotes: 1

Related Questions