Android calls webservice to upload images

Source: Internet
Author: User

After taking a picture of a project recently, we need to upload the image to the server, and the server is written by the cs webservice. Generally, webservcie communication is called to transmit strings in xml or json format. I have never uploaded files like images. Baidu used many methods, and finally used the android client to read the image to be uploaded using the io stream, and encoded it with Base64 into a character string with throttling, call webservice to upload the string as a parameter to the server. The server decodes the string and stores it in the corresponding path. The key to the entire upload process is to transmit data using byte stream strings. The download process is the opposite of the upload process. You can change the codes of the server and client.

As described in the following article:

 
There are so many code. The process is: upload an image on android sdcard to the images folder under the server.
Note: This is just a demo. There is no UI interface, and the file path and file name are all written to death. During the runtime, you just need to change it accordingly.
1. Read images from android sdcard.
Public void testUpload (){
Try {
String srcUrl = "/sdcard/"; // path
String fileName = "aa.jpg"; // file name
FileInputStream FCM = new FileInputStream (srcUrl + fileName );
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
Byte [] buffer = new byte [1024];
Int count = 0;
While (count = FS. read (buffer)> = 0 ){
Baos. write (buffer, 0, count );
}
String uploadBuffer = new String (Base64.encode (baos. toByteArray (); // Base64 encoding
String methodName = "uploadImage ";
ConnectWebService (methodName, fileName, uploadBuffer); // call webservice
Log. I ("connectWebService", "start ");
FCM. close ();
} Catch (Exception e ){
E. printStackTrace ();
}
}
ConnectWebService () method:
// Call webservice using ksoap2
Private boolean connectWebService (String methodName, String fileName, String imageBuffer ){
String namespace = "http: // 134.192.44.105: 8080/SSH2/service/IService"; // namespace, that is, the server interface. Note: The suffix is not added. wsdl,
// On the server side, I use x-fire to implement the webservice interface.
String url = "http: // 134.192.44.105: 8080/SSH2/service/IService"; // The url
// The following is the call process. If you do not understand it, see the relevant webservice documentation.
SoapObject soapObject = new SoapObject (namespace, methodName );
SoapObject. addProperty ("filename", fileName); // parameter 1 image name
SoapObject. addProperty ("image", imageBuffer); // parameter 2 image string
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope (
SoapEnvelope. VER10 );
Envelope. dotNet = false;
Envelope. setOutputSoapObject (soapObject );
HttpTransportSE httpTranstation = new HttpTransportSE (url );
Try {
HttpTranstation. call (namespace, envelope );
Object result = envelope. getResponse ();
Log. I ("connectWebService", result. toString ());
} Catch (Exception e ){
E. printStackTrace ();
}
Return false;
}
2. Webservice code on the server:
Public String uploadImage (String filename, String image ){
FileOutputStream fos = null;
Try {
String toDir = "C :\\ Program Files \ Tomcat 6.0 \ webapps \ SSH2 \ images"; // storage path
Byte [] buffer = new BASE64Decoder (). decodeBuffer (image); // decodes the image string uploaded by android
File destDir = new File (toDir );
If (! DestDir. exists () destDir. mkdir ();
Fos = new FileOutputStream (new File (destDir, filename); // Save the image
Fos. write (buffer );
Fos. flush ();
Fos. close ();
Return "image uploaded! "+" Image path: "+ toDir;
} Catch (Exception e ){
E. printStackTrace ();
}
Return "An error occurred while uploading the image! ";
}
Call the testUpload () method for unit tests on android. If you see green bars, the call is successful! On the server, you can see the picture you uploaded ....
Of course, this demo is very simple, and there is no beautiful UI or something, but this is the process for android to call webservice to upload images. The same applies to downloading data from the server to android. Welcome to exchange and learn ....

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.