Skip to content Skip to sidebar Skip to footer

How To Upload Images From Android Sqlite To Local Server Folder Using Json

Am Having Images and Text in android Sqlite database.and My Need is to uplaod those images and text to my system folder via Local Server using Json..I dont have any idea , Please

Solution 1:

You cannot simple put your text and images directly on server from you app. You need to have Server side logic to handle the requests. While searching, you must have found lot of PHP server side code, because PHP is the easiest to get started.

The only way i could think of to send images along with text in JSON format is to convert images to Base64. On the server side, you have to convert them back to images and save.

Also, don't save images directly in Sqlite database. It's not designed to handle large BLOB's (images or any other binary data). You can save the images to file system and save the paths in database.

Edit:

You can use the following code to send JSON string from Android to PHP

on Android

JSONObjectjsonObject=newJSONObject();
jsonObject.put("text", "YOUR_TEXT_HERE");
jsonObject.put("image", "YOUR_BASE64_IMAGE_HERE");

StringEntitypostBody=newStringEntity(jsonObject.toString());

HttpClienthttpClient=newDefaultHttpClient();
HttpPosthttpPost=newHttpPost("http://YOUR_URL_HERE");

httpPost.setEntity(postBody);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");

httpClient.execute(httpPost);

PHP code

$raw_json_string = file_get_contents('file://input');
$json = json_decode($raw_json_string);

// Copied shamelessly from: http://stackoverflow.com/a/15153931/815540functionbase64_to_jpeg($base64_string, $output_file) {
    $ifp = fopen($output_file, "wb"); 
    $data = explode(',', $base64_string);
    fwrite($ifp, base64_decode($data[1])); 
    fclose($ifp); 
    return$output_file; 
}

$text = $json->text;
$base64_image = $json->image;

base64_to_jpeg($base64_image, 'PATH_YOU_WANT_TO_SAVE_IMAGE_ON_SERVER');

That's all there is to it....!

haven't tested the code though

Post a Comment for "How To Upload Images From Android Sqlite To Local Server Folder Using Json"