Im trying to retrieve images from my populated database and put them into viewpager2.This works when i insert images in db viewpager2 shows them but when i try to load them when i first enter the activity, the app crashes.
Expected to load images successfully because i already used the same method for loading from db but here when it comes to cursor.moveToFirst() it shows binary code doesnt match the system code.
private void loadDB(List list){
SQLiteDatabase db = imagesDB.getReadableDatabase();
SharedPreferences sharedPreferences = getSharedPreferences(“mesto”,MODE_PRIVATE);
mesto = sharedPreferences.getString(“mesto”,””);
String tablename = "";
switch (mesto){
case "KruskaPab":
tablename = "KruskaPabImages";
break;
case "SalsaBar":
tablename = "SalsaBarImages";
break;
case "Duomo":
tablename = "DuomoImages";
break;
case "BasementBar":
tablename = "BasementBarImages";
break;
}
Cursor cursor = db.query(tablename,null,null,null,null,null,null);
if(cursor.moveToFirst()){
do {
byte[] imageBytes = cursor.getBlob(cursor.getColumnIndexOrThrow("image"));
// Convert the byte array to Bitmap
Bitmap imageBitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
selectedImages.add(imageBitmap);
}while(cursor.moveToNext());
viewPageAdapter = new ViewPageAdapter(selectedImages);
viewPager2.setAdapter(viewPageAdapter);
cursor.close();
db.close();
}
}
Yokoso99 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.