androCoder-BD androCoder-BD - 4 months ago 17x
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.



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:


    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.