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

2
  • the key point is: you should not store fikes in database. save on normal filesystem an keep reference in db Commented Jan 4, 2014 at 22:28
  • yes, I know, but I wanted to keep my picture more secure, this is why I thought about this. Commented Jan 4, 2014 at 22:30

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

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 I would insert it as a Blob Type for SQL. You can convert the image to a Byte Array, see stackoverflow.com/questions/3211156/…
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.

1 Comment

sorry to say, but you might have not understood my question properly. :). Want to know how to store those in a database.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.