I need to store in Database images chosen from gallery. My firt idea was convert Bitmap to String and store String in Database, but now I am reading other post: saving image clicked from camera or loaded from gallery in database and retrieving it and there is suggested using byte array. Could someone explain me diffrence, which idea is better? Maybe something else? I just start, but I would like to write it possibly correctly.
Asked
Active
Viewed 103 times
-1
-
You could store the images on a server and store the URL to the images in your database? – rtc11 Jul 29 '14 at 11:32
-
In that case, I prefer to save all image in my database – pupilx Jul 29 '14 at 11:38
-
possible duplicate of [How to save images into Database](http://stackoverflow.com/questions/7512019/how-to-save-images-into-database) – Jul 29 '14 at 12:00
2 Answers
0
The standard way to store an image as a byte[]
in a BLOB
field. Another possibility - with some overhead - is to store a Base64
-encoded string.

injecteer
- 20,038
- 4
- 45
- 89
-
Thank you for your comment. I wonder which one should I choose. About which exactly overhead are you saying? – pupilx Jul 29 '14 at 11:36
-
-
the good thing about Base64 is, that the encoded string can be safely transmitted over the network, and receiving side would not get some encoding- or format- clash – injecteer Jul 29 '14 at 11:43
-
Ok. Maybe it is case for another question, but could you advise me, doing it this way I will duplicate data. But saving for example only package where is image I risk deleting photos from memory and lack of access. My project contains a lot of photos, so I wonder which idea is the best – pupilx Jul 29 '14 at 11:53
-
I think the post you referenced is good enough for your case. What do you mean by "duplicate data" and "saving only package"? – injecteer Jul 29 '14 at 12:55
-
It means that I will have save image in gallery and in my database (but possible that I don't understand it correctly :) ) ; only package for example- store place where is photo and then- retrieve it – pupilx Jul 29 '14 at 13:12
-
yes, if the pictures are stored in the file-system already, there's no need to replicate them in the db. In this case you should simply store the `URI`s – injecteer Jul 29 '14 at 13:29
-
Exactly, but when I save URI and in future delete image from gallery I lost it in my app too, ya? – pupilx Jul 29 '14 at 13:37
-
yes, this is the decision you have to make: duplicate data which is safe, or eventually have dead links – injecteer Jul 29 '14 at 13:40
0
You can use the Base64 Android class:
String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
You'll have to convert your image into a byte array though. Here's an example:
Bitmap bm = BitmapFactory.decodeFile("/path/to/image.jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
If you're using an older SDK library (because you want it to work on phones with older versions of the OS) you won't have the Base64 class packaged in (since it just came out in API level 8 aka version 2.2).
Check this article out for a work-around:
http://androidcodemonkey.blogspot.com/2010/03/how-to-base64-encode-decode-android.html

VVB
- 7,363
- 7
- 49
- 83