开发者

Fetch images and metadata from PHP server to android

My android app needs to fetch some info from a php server that hosts a MySQL database. It works fine so far. The server encodes the info into JSON and sends it and then I can parse it well.

But now I need to also fetch an image together with each row of info I get from the DB. The info I'm getting has a field where it specifies the path where the correspondent image is located in the filesystem.

So, once I get the paths of the images, how do I read them so that I can send them together with the info rows obtained? Can I JSON encode the images together with the info? Or should I read them one by one开发者_JS百科 with readfile once I have the info in the android app? If it can be done with JSON, how do you parse the data of the image afterwards? Can you provide an example?


One way to get an image in textform would be to use base64. I have used it with several webservices and there is decoders for Android out there, actually. There is one in the source code since API level 8. http://developer.android.com/reference/android/util/Base64.html but since I want to target other levels I include it myself. One easy way would then be to save the image in a database instead as a file.


This is what I did to fetch the images together with the data. I post it so that if it can help someone.

This is the part of the PHP script that sends the data in 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's 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 app, after parsing the JSON, you can save the base64 string as an image to the file system like this:

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


for downloading 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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜