如何使用JSON将图像从Android Sqlite上传到本地服务器文件夹

问题描述:

我在android Sqlite数据库中有图像和文本.我的需要是使用Json通过本地服务器将这些图像和文本上载到我的系统文件夹中.我没有任何想法,请给我任何想法和编码或帮助我例如..我在这个主题上进行了搜索..大部分基于Php Server的所有链接..如果有人帮助我,在此先感谢.

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..

您不能简单地将文本和图像直接从应用程序直接放置在服务器上.您需要具有服务器端逻辑来处理请求.搜索时,您必须找到许多PHP服务器端代码,因为PHP是最容易上手的.

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.

我想到发送图像和JSON格式的文本的唯一方法是将图像转换为Base64.在服务器端,您必须将它们转换回图像并保存.

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.

此外,不要将图像直接保存在Sqlite数据库中.它不是为处理大型BLOB(图像或任何其他二进制数据)而设计的.您可以将图像保存到文件系统,并将路径保存在数据库中.

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.

您可以使用以下代码将JSON字符串从Android发送到PHP

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

在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代码

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

// Copied shamelessly from: http://*.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....!

虽然还没有测试代码