androCoder-BD androCoder-BD - 5 months ago 35
Android Question

how to insert an image in mysql -android

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

Answer

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.