How to save base64 Android to MySQL database?

1

I am sending an image to laravel API using retrofit

D/NetworkManagementSocketTagger: tagSocket(74) with statsTag=0xffffffff, statsUid=-1

I/mple1.messegin: Background concurrent copying GC freed 21904(27MB) AllocSpace objects, 14(3MB) LOS 
objects, 50% free, 18MB/37MB, paused 402us total 102.147ms

E/FF1: 500 Response{protocol=http/1.1, code=500, message=Internal Server Error, 
url=http://192.168.1.8/company_messenger/public/apistore}

I see the first line then after a moment the other two lines appear

I get these errors I don't know what exactly is the problem

  1. I convert the image to base64

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); // bm is the bitmap object
    byte[] b = baos.toByteArray();
    String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
    
  2. In laravel I receive it in column with type TEXT

PS: when I send just a small string instead of the image decoded it is stored correctly

android
mysql
laravel
base64
asked on Stack Overflow Sep 6, 2020 by Ehsan Hadri • edited Sep 7, 2020 by marc_s

2 Answers

0

which type of column do you have?

it must be binary, to store such kind of data

See this discussion about saving images in databases as encodec64 or binary Storing image in database directly or as base64 data?

Besides storing big amounts of data , increases the load massively and usually it is enough to save the images on the drive and only save the path

The maxima amount of data you can save in a field for BlOB and TEXT

TINYBLOB, TINYTEXT       Up to 255 bytes
BLOB, TEXT               Up to 64 Kb    
MEDIUMBLOB, MEDIUMTEXT   Up to 16 Mb
LONGBLOB, LONGTEXT       Up to 4 Gb

As you can see TEXt can only hold 64 KB on data, and your image is probably bigger than that also encoding it in Base 54, increases the size even further

answered on Stack Overflow Sep 6, 2020 by nbk • edited Sep 7, 2020 by nbk
0

If you are storing base64, text won't hurt, but I would make such a column binary.

But a TEXT or BLOB (the corresponding binary type) column can have a maximum of 65535 bytes (43690 before base64 encoding). For up to 16 megabytes, use a MEDIUMTEXT or MEDIUMBLOB type. For up to 4 gigabytes, use a LONGTEXT or LONGBLOB type.

answered on Stack Overflow Sep 7, 2020 by ysth

User contributions licensed under CC BY-SA 3.0