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(); } }
source share