blankwall
blankwall

Reputation: 353

How to get images fromMySQL for android

I have been working hard in this app and have come into a problem of late. I am trying to store images (or paths to those images) in a database on my local network and retrieve them from my android application. I have been able to use PHP and JSON to retrieve text information but am not sure where to start for images. Should I store the actual images in the MySQL database or paths to images? How would I be able to keep them in a neat format? Any information you guys can offer is greatly appreciated.

Upvotes: 0

Views: 795

Answers (2)

Noman
Noman

Reputation: 249

U can save your image in database in form of byte array . and when u want to retrieve it u can get it. or there are Base64 conversion of images.

Upvotes: 0

SISYN
SISYN

Reputation: 2259

Just store the path in a database. Create a simple table that might have the following fields:

path - filepath or filename to the image on the server id - numeric auto_increment id that is useful while retrieving or identifying images width - optional, but might come in handy because storing height/width is quicker than calculating it upon retrieval height - same deal as width category - useful if you want to display more than one image a time, this could help you group them and decide which ones belong together. give the same category value for images that belong together

In order to retrieve the information for a certain picture, you could call it in one of two ways. The most likely solution would be to retrieve images based on their filename.

$con = mysql_connect('host', 'user', 'pass');
$db = mysql_select_db('mydb');

$imgName = 'picture.jpg';
$getImg = mysql_query("SELECT * FROM mypics WHERE path='" . $imgName . "' ORDER BY id DESC LIMIT 1");
$imgSrc = mysql_result("path", 0, $getImg);
echo '<img src="' . $imgName . '" />';

Of course this seems a bit useless, because we already know the filename to begin with because we are storing it in $imgName and using it to search the database for the entry, but you could search based on ID, category, width, or height all the same way. However, this will only retrieve the first image returned. Likewise, the images are being sorted by most recent because of the "ORDER BY id DESC" call. If you want to be able to retrieve all the images returned by the query simply remove the "LIMIT 1" from the query and change the "0" in mysql_result() to whichever entry you want to retrieve. Note that results are 0-indexed like arrays, so if you want the 5th result, you would get result 4.

If you want to display all images in a category, you can do something like this:

$con = mysql_connect('host', 'user', 'pass');
$db = mysql_select_db('mydb');

$getImgs = mysql_query("SELECT * FROM mypics WHERE category='android' ORDER BY id DESC");
while( ($i = mysql_fetch_array($getImgs) ) {
    echo '<img src="' . $i['path'] . '" />';
}

This would display all images from the category "android", starting with the most recent. Hope that helps.

Upvotes: 1

Related Questions