0

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.

13
  • do not use BLOBS for storing big images - use normal files instead and store the path to them in your sqlite db Commented Sep 27, 2017 at 8:59
  • At first place why you need to store image in Database instead of image path? Commented Sep 27, 2017 at 11:19
  • @pskink please see edit2 Commented Sep 28, 2017 at 5:20
  • @Aks4125 please see edit2 Commented Sep 28, 2017 at 5:20
  • so now are you using glide/picasso/volley/whatever for background image loading and still your app is doing too much work on its main thread ? Commented Sep 28, 2017 at 5:24

1 Answer 1

1

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(); } 
Sign up to request clarification or add additional context in comments.

2 Comments

but main problem is you are using bloob in database. so the ur application is "lag"
please see edit2, I have changed it to get/save the file path instead of BLOB.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.