1

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 help me anyone by giving ideas and coding or example .. I googled this topic ..Most of all link based on Php Server only ..Thanks in Advance if anyone help me..

Android
  • 35
  • 2
  • 7

1 Answers1

0

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

JSONObject jsonObject = new JSONObject();
jsonObject.put("text", "YOUR_TEXT_HERE");
jsonObject.put("image", "YOUR_BASE64_IMAGE_HERE");

StringEntity postBody = new StringEntity(jsonObject.toString());

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("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/815540
function base64_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

Community
  • 1
  • 1
bhargavg
  • 1,383
  • 9
  • 12
  • thanks for your response,server side logic means? webservice? and am having that images in sd card also . .will you please give some exmaple for covert sd card images to Base64 and send to system folder via local server.. – Android Aug 14 '14 at 06:12
  • Yes, by server side logic i mean web services. [Check this](http://stackoverflow.com/q/4830711/815540) to convert images into `Base64` – bhargavg Aug 14 '14 at 06:14
  • Thanks dude,I read that Base64 link .Then Please how to upload to system folder via local server.. – Android Aug 14 '14 at 06:24
  • Thanks, sorry Am having these php coding ..I dont want This php coding.I want to send data to my local server using json only.. – Android Aug 14 '14 at 06:51
  • @Android, what json coding are you referring to? `JSON` creation and `POST`ing in android was shown above. – bhargavg Aug 14 '14 at 08:10
  • You cannot run it from Android Eclipse, you need to have LAMP/WAMP setup on your machine. Search google for how to run PHP scripts – bhargavg Aug 14 '14 at 10:06
  • Thanks For Your patient reply.. Now i wants to learn about how to use php file..after learning will come to you.. – Android Aug 14 '14 at 10:27