Android: 上传图片到服务器

本文介绍了如何使用Android设备将图片转换为Base64字符串,并通过HTTP请求将其发送到服务器。服务器端通过PHP解码Base64字符串,恢复图片原始数据并保存。

PHP is used at server side to serve image. Following is a scenario that indicates how image transforms from one format to another format and finally back to original format.

Now, you understand that actually, we are sending String from android device to server not the image. Android.util package does not support the functionality of base64 encoder and decoder. So what, here is the solution.

Just go to link, http://iharder.sourceforge.net/current/java/base64/ and download the zip. Extract it and find the Base64.java file. Copy it and Past it on your project. Copy it into appropriate “src” folder.
Now, refresh Package Explorer,so that Base64.java file can be included. you will get the following error “The declared package does not match the expected package”. Fix this by opening the class file and adding a package declaration that matches your project. Following is the screenshot.

Now, following is the java code that converts the image into String and sends it to Server. You can get the pdf from here.

public class upload extends Activity {

InputStream is;

@Override

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

setContentView(R.layout.main);

Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),

R.drawable.a1);

ByteArrayOutputStream bao = new ByteArrayOutputStream();

bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);

byte [] ba = bao.toByteArray();

String ba1=Base64.encodeBytes(ba);

ArrayList<NameValuePair> nameValuePairs = new

ArrayList<NameValuePair>();

nameValuePairs.add(new BasicNameValuePair("image",ba1));

try{

HttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new

HttpPost("http://10.0.2.2:80/android/base.php");

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

HttpResponse response = httpclient.execute(httppost);

HttpEntity entity = response.getEntity();

is = entity.getContent();

}catch(Exception e){

Log.e("log_tag", "Error in http connection "+e.toString());

}

}

}

Server Side:

Now install the server. Create the file base.php. Create another file empty file test.jpg. Make sure that path of this two file will be same. At server side base.php will be executed. This PHP Script will open test.jpg file and write the binary data.
So, now you understand that test.jpg is the result of this example. Here is the PHP code, you can get the pdf from here.

<?php

$base=$_REQUEST['image'];

echo $base;

// base64 encoded utf-8 string

$binary=base64_decode($base);

// binary, utf-8 bytes

header('Content-Type: bitmap; charset=utf-8');

// print($binary);

//$theFile = base64_decode($image_data);

$file = fopen('test.jpg', 'wb');

fwrite($file, $binary);

fclose($file);

echo '<img src=test.jpg>';

?>

Do not forget to insert following permission in AndroidManifest.xml.

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

原文链接:

http://blog.sptechnolab.com/2011/03/09/android/android-upload-image-to-server/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值