0

Recently I was working on MySql and Sql, where I found I can almost insert everything primitive type data but is it possible to store a jpeg/jpg/bmp/png (image)file in a database. If so then can you please write the code. Don't need to explain the whole thing just the key point.

Thanks

androCoder-BD
  • 498
  • 7
  • 13

3 Answers3

1

1 Perhaps convert the image to a Byte Array (if it's not bmp, then convert it), then store it in MySql as a Blob type, then when reading it, you can create a Bitmap through the code (I don't know what language you're using) with the Byte Array with what you read, or perhaps just get the code associated with the image and store that and read it.

Converting an image to a Byte Array

2 Use an image hosting API (like Imgur) and have the user's image upload to that site, and just read the URI from the database whenever you want to use it.

Imgur API, Android Example

Community
  • 1
  • 1
Cilan
  • 13,101
  • 3
  • 34
  • 51
  • Am using java,for android development. However, the database execution code should remain same. So, shall I just insert the **Bitmap** as a Blob Type? or Do I need to convert it into something else(maybe number or I don't know)? – androCoder-BD Jan 04 '14 at 22:34
  • @androCoder-BD I would insert it as a Blob Type for SQL. You can convert the image to a Byte Array, see http://stackoverflow.com/questions/3211156/how-to-convert-image-to-byte-array-in-java – Cilan Jan 04 '14 at 22:36
0

there is 3 cases how to make it (that is i know)

  1. Save path from SD card like String in database
  2. Save int value if image is in res folder of your app.
  3. Save url like String if image is in Internet.
Kostya Khuta
  • 673
  • 2
  • 7
  • 21
  • sorry to say, but you might have not understood my question properly. :). Want to know how to store those in a database. – androCoder-BD Jan 04 '14 at 22:40
0

Sorry for the late response. However, I found a way to convert the image to String and then store it in the mysql. This answer is just for those who are still looking for a easy solution:

code:

    Bitmap bm = BitmapFactory.decodeFile("/path/to/myImage.jpg");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
    byte[] b = baos.toByteArray(); 

and then:

String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);

Then the encodedImage which is a String format, I can insert into the db. Hope that helps other who are still looking for the answer.

androCoder-BD
  • 498
  • 7
  • 13