Retrieving Images and Metadata from a PHP Server on Android

My Android application should get some information from the php server hosting the MySQL database. While this is working. The server encodes the information in JSON and sends it, and then I can parse it well.

But now I also need to get an image along with each line of information that I get from the database. The information that I receive has a field that indicates the path in which the corresponding image is in the file system.

So, as soon as I get the paths to the images, how do I read them so that I can send them along with the received info lines? Can I JSON encode images along with information? Or should I read them one by one with readfile as soon as I have the information in an Android application? If this can be done using JSON, how will you analyze the image data after that? Can you give an example?

+4
source share
3 answers

This is what I did to get the images along with the data. I am sending him so that he can help someone.

This is the part of a PHP script that sends data on the server side:

$i=0; //$sql contains the result from the query of the data while($row=mysql_fetch_array($sql)){ $output[]=$row; //Here we fetch the image from the files table $query = "SELECT path, type FROM FILES WHERE poi_id = '" . mysql_real_escape_string(trim($output[$i][0])) . "'"; $r = mysql_query($query); $img = mysql_fetch_array($r); $bin = base64_encode_image($img[0]); //Let append the image and the type to the data output. $output[$i]['img'] = $bin; $output[$i]['type'] = $img[1]; $i++; } //This method sends the response to the Android app //You can just use echo(json_encode($output)); RestUtils::sendResponse(200, json_encode($output), 'application/json'); 

And then in the Android application, after parsing the JSON, you can save the base64 string as an image in the file system as follows:

 public void createImage(String image, String name){ try{ byte[] imageAsBytes = Base64.decode(image.getBytes(), Base64.DEFAULT); Bitmap img_bitmap = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length); FileOutputStream fos = openFileOutput(name, Context.MODE_WORLD_READABLE); img_bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); } catch(Exception e){ e.printStackTrace(); } } 
0
source

One way to get the image in text format would be to use base64 . I used it with several web services, and there are actually decoders for Android. There is one of them in the source code from API level 8. http://developer.android.com/reference/android/util/Base64.html , but since I want to target other levels, I enable it myself. One simple way would be to save the image in the database instead as a file.

+2
source

to download image from path

  public void DownloadImageFromPath(String path){ InputStream in =null; Bitmap bmp=null; ImageView iv = (ImageView)findViewById(R.id.img1); int responseCode = -1; try{ URL url = new URL(path);//"http://192.xx.xx.xx/mypath/img1.jpg HttpURLConnection con = (HttpURLConnection)url.openConnection(); con.setDoInput(true); con.connect(); responseCode = con.getResponseCode(); if(responseCode == HttpURLConnection.HTTP_OK) { //download in = con.getInputStream(); bmp = BitmapFactory.decodeStream(in); in.close(); iv.setImageBitmap(bmp); } } catch(Exception ex){ Log.e("Exception",ex.toString()); } } 
0
source

All Articles