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;@Overridepublic 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 = newArrayList<NameValuePair>();nameValuePairs.add(new BasicNameValuePair("image",ba1));try{HttpClient httpclient = new DefaultHttpClient();HttpPost httppost = newHttpPost("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 bytesheader('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/

更多相关文章

  1. Android(安卓)Firebase Dynamic Links 动态链接使用心得
  2. android HTTPURLConnection解决不能访问HTTPs请求
  3. Android(安卓)网络链接,不要忘记添加网络权限。
  4. android切换效果、Flutter信息类App、仿饿了么点餐、仿爱壁纸应
  5. Android中TextView中加图片,超链接,部分字或者背景变色。。。不断
  6. Android指定调用系统自带浏览器打开链接
  7. ADT下载地址(含各版本)(转)
  8. Android中TextView中加图片,超链接,部分字或者背景变色。。。
  9. Android样式开发

随机推荐

  1. Kotlin在AndroidStudio中的环境搭建以及
  2. 【Android自助餐】Handler消息机制完全解
  3. Android数据库高手秘籍(零)——前言[转载
  4. android 系统服务
  5. adb设备连接以及文件拷贝
  6. Android与HTML JavaScript交互
  7. Android 游戏开发的一些基础和经验
  8. android sqlite 介绍
  9. Android模拟器
  10. Android如何使布局中图(ImageButton)和文字