Android Question How to Insert a picture into MySQL database

ProjectGroup19

Active Member
Licensed User
Greetings All,
Im trying to upload a picture into my database.
I used content chooser to select a picture from the phone device but I don't know how to Insert the selected picture in my database.

I'm using okhttp2 with php.

I would be very glad if you can help me with it.
 

drgottjr

Expert
Licensed User
Longtime User
side point first: generally, you should keep the image in a separate file and link to it from a column in the db.

if you have to keep the image in the db, it's stored as longblob type.
once your php script has the image file, it has to break it down into bytes and store the bytes in the longblob column.

i'm guessing you already can handle the okhttp part. the php part is easy to find online. search for "mysql php blob image" at the Big G. plenty of example scripts.
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Longtime User
You could also consider converting the image file to a base64 text string. This base64 text string can simply be scanned by an anti-virus checker. This prevents any blockages by the anti-virus software and a base64 picture in an email message is simply displayed in the email message.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
i think images are converted to base64 as part of the upload. at least for multi-part. i don't know what ProjectGroup19 includes in his uploads, presumably more than just an image
 
Upvote 0

ProjectGroup19

Active Member
Licensed User
side point first: generally, you should keep the image in a separate file and link to it from a column in the db.

if you have to keep the image in the db, it's stored as longblob type.
once your php script has the image file, it has to break it down into bytes and store the bytes in the longblob column.

i'm guessing you already can handle the okhttp part. the php part is easy to find online. search for "mysql php blob image" at the Big G. plenty of example scripts.
Please, can I get an example on how to upload the image in a separate file and link to it from a column in the db.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
based on what you said before, you are already uploading the image. presumably, successfully. yes? so you already know how to do that part.

when the image arrives you save it somewhere on your server. can you do that? what's its name? where did you save it? (eg "/dbfiles/section1/123456.jpg")
in your db, you have a col used for each upload right? you create a col called "filename varchar(256)". put the location in that field. later, when you do a select, you get that col. it points to a location. you get the image file at that location.

in databases, it's not unusual for a col to point to other tables and files. (for example, an id can be used to select from many tables that share the same id).
pretend you didn't have a database. if you uploaded an image to your server, what would you do with it? how would you keep track of where you put the files? that's all you're doing with the db. you store the name of the location. the actual image is somewhere else.

you can certainly store it inside the database, but you have to convert it to a blob first. but imagine if you had several databases that could all refer to the same image. you would have to keep a copy of the same image in each database or table! no; you keep 1 copy of the image, but you store its location in all the databases and tables. it's a way of avoiding duplication.

you don't need a customer's name and address in every table. you have 1 table with names and addresses, and you use an id in the other tables.
for a small db system, it probably doesn't matter. but you will have to convert the image data to a blob before storing the image in the db. php will have such a function.
 
Upvote 0
Top