please help me in store and retrieving of images in sqlite and i had used the tab layout for my app..My main activity is one of the tabs fragment please help me in write the exact code..
Asked
Active
Viewed 641 times
-3
-
1check this lnk: http://stackoverflow.com/questions/35917673/android-save-and-get-image-from-sqlite-database/40858225#40858225 – Noorul Dec 26 '16 at 12:09
-
how to use my images for the bitmap – PaulWalker Dec 26 '16 at 12:12
-
how i mange this bitmap with my 20 images – PaulWalker Dec 26 '16 at 12:21
-
saving an image in SQLite is bad idea. So, for your question, i have answered. that's all. if you use more image you will face the heap size error or OutOfMemoryError. So, dont do this. – Noorul Dec 26 '16 at 12:23
-
yeah but i want to create an app based on radio do i want to just show the program image while in current show is running and also in shows list i.e i have images as my dynamic images.. – PaulWalker Dec 26 '16 at 12:46
1 Answers
0
Get bitmap from ImageView:
Bitmap bitmap = ((BitmapDrawable)ivImage.getDrawable()).getBitmap();
If you have bitmap of your image then go with this one:
Save in database:
public void insertImg(Bitmap img) {
byte[] data = getBitmapAsByteArray(img); // this is a function
//Now save this byte array data as blob in database
}
public static byte[] getBitmapAsByteArray(Bitmap bitmap) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0, outputStream);
return outputStream.toByteArray();
}
Retrieve from database:
public Bitmap getImage() {
//fetch byte array from your query cursor
byte[] imgByte = cur.getBlob(0);
return BitmapFactory.decodeByteArray(imgByte, 0, imgByte.length);
}
Set retrieved bitmap from database to ImageView:
ivImage.setImageBitmap(getImage());
But saving images as byte array in database is not a good practice. Because saving and retrieve images as byte array may be give an issue of outOfMemory. So if you have image url which you are getting from server then save that url as a string in database and retrieve it with same column where you have saved.
Or if you have any drawable image then save that drawable image name in database and retrieve with the same.

Ready Android
- 3,529
- 2
- 26
- 40