James andresakis James andresakis - 1 year ago 58
MySQL Question

Most efficient method to pull multiple images from a MySQL db to an Android device

In short, my application needs to pull images from a MySQL db. Since the images pulled will depend on the users location I can't just hardcode image file paths.

So far I've tried using just a PHP script that should allow the app to pull the images from the table into my db but I haven't had any successful results.

In my app this is what I'm using to pull and decode the stream into a bitmap that the phone/app can use:

public static void createIcons(Resources res) throws IOException {
String url1 = " http://mysite.iscool/file-get.php ";
URL ulrn = new URL(url1);
HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
InputStream is = con.getInputStream();
//end of lines added


Since I can't seem to return an image to the device from the db using this so far I was wondering what sort of direction I should be going in. I've seen lots of different methods on here explained but I'm not too sure if the method I'm trying to use is even going to work for the purpose I need it to.

Also, I have seen mention of a REST service and other ways to extract images from a mysql db table. So does any one have an suggestions/tutorials/examples of extracting multiple images from a MySQL database?

By the way here is the code I'm using to try to return an image to the device:

// access server
$dbQuery = "SELECT File FROM mark_image_store where imageid =(SELECT MAX(imageid) from

// selecting into a single element variable
// stores only the first field which is the file data
$result = mysql_query($dbQuery) or die(mysql_error());
//reserved for when there is file ext data available.
//to format http headers
//Do not need for use by the phone.
//header("Content-type: image/jpeg");
//if ($ext = "png")
// use imagejpeg, imagepng?
header("Content-type: image/png");
print mysql_result($result, 0);

So after looking at my PHP and Android code can someone please give me some advice on which direction I should be going in next. Should I rework my Android side code and go with a REST service or Webservice or is a PHP script server side enough to handle the image returns.

Any advice/tutorials/or examples would be much appreciated thanks.

Answer Source

You can extract multiple images easily with a single query. But you'll only be able to TRANSFER one image a time, unless you use some kinda of encapsulation like .zip.

If you're serving up the images via HTTP, you'll definitely only be able to serve one picture at a time. There's some tricks where you can serve up a single image with a series of smaller images tiled within it, and display them in restricted-sized divs as tiles/sprites, but that'd require client intervention.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download