본문 바로가기

«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

올해는 머신러닝이다.

android image upload with php 본문

Android/Tip&Tech

android image upload with php

행복한 수지아빠 2011. 8. 2. 10:56
출처 : http://blog.sptechnolab.com/2011/03/09/android/android-upload-image-to-server/

Android: Upload image to Server

I have searched lot on Internet that how to upload image from android device to server. As a result of that, I am posting this tutorial. At server side, I used PHP script. So, you need to learn basic of PHP, because PHP is used at server side to store 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