问题
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..
回答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
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
来源:https://stackoverflow.com/questions/25300899/how-to-upload-images-from-android-sqlite-to-local-server-folder-using-json