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.
Image may be NSFW.
Clik here to view.
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.
Image may be NSFW.
Clik here to view.
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>
Following are the links of other Post on Android.
Android : ContextMenu & SubMenu
Android : Connecting to MySQL using PHP
Android : Sliding Drawer
Android Countdown Timer
Getting started with Android Map View: Displaying a location by Marker
Simple Progress Bar in Android using Thread
Android listview with image and text
Manage & Developed by Sptechnolab.com