Android programming to implement picture upload and download function Example _android

Source: Internet
Author: User
Tags web services

This article describes the implementation of Android programming image upload and download functions. Share to everyone for your reference, specific as follows:

In the implementation of an Android Web services client, such as microblogging, forum clients, often used to upload and download images. Here describes how to use httpclient to achieve the upload and download of pictures.

1 picture upload : Upload the picture, first get the path of the picture, create the file, and convert the picture into a stream of bytes written to the request, and send the requests.

Client code:

File File = new file (IMAGEURL);
String Httpurl = httpdomain+ "Addimageservlet" + "? gid=" +gid;
HttpPost request = new HttpPost (httpurl);
HttpClient httpclient = new Defaulthttpclient ();
fileentity entity = new Fileentity (file, "Binary/octet-stream");
HttpResponse response;
try {
  request.setentity (entity);
  Entity.setcontentencoding ("Binary/octet-stream");
  Response = Httpclient.execute (request);
If the return state is 200, obtain the returned result
if (Response.getstatusline (). Getstatuscode () ==httpstatus.sc_ok) {
...//Picture uploaded successfully
}
}
catch (Exception e) {
}

The server-side work is to receive the byte stream, write to the file, save the file in the appropriate folder in the server, record the path to the file, and write the picture file path to the database for saving.

Server-side code:

Get news id String gid = request.getparameter ("gid");
String FilePath = Getrealpath (Request) + "\\userpic\\";
Defines the maximum byte int max_size = 102400 * 102400 of the uploaded file;
The declaration file reads into class datainputstream in = NULL;
FileOutputStream fileout = null;
Gets the data type that the client uploads String ContentType = Request.getcontenttype (); if (Contenttype.indexof ("Binary/octet-stream") >= 0) {//Read the uploaded data in = new DataInputStream (Request.getinputstream ()
  );
  int formdatalength = Request.getcontentlength ();
    If the picture is too large if (Formdatalength > Max_size) {String errormsg= ("The number of bytes uploaded can not exceed" + max_size);
    Out.println (ERRORMSG);
  return;
//Save the uploaded file data byte databytes[] = new Byte[formdatalength];
int byteread = 0;
int totalbytesread = 0; The uploaded data is saved in the byte array while (Totalbytesread < formdatalength) {Byteread = In.read (Databytes,totalbytesread,
Formdatalength);
 Totalbytesread + = Byteread;
 String fileName = FilePath + gid+ ". png";
Check that the directory for the uploaded file exists with file Filedir = new file (FilePath); if (!filedir.exists ()) {FILEDIR.MKDIRS ();
//Create the Write class Fileout = new FileOutputStream (filename) of the file;
Save the file Data Fileout.write (databytes);
Fileout.close ();

 Save the path name of the file ...

2 Picture Download : First get the picture of the network image address, send a request, the server will return the image of the stream of bytes, using the Bitmapfactory.decodestream () method of the byte flow into the picture and return. The specific code is as follows:

/Get the picture on the network public Bitmap getgossipimage (String gid) {string httpurl = httpdomain+ "us
    erpic/"+gid+". png ";
    Bitmap Bitmap = null;
    HttpGet HttpRequest = new HttpGet (Httpurl);
    Get HttpClient object HttpClient httpclient = new Defaulthttpclient ();
      try {//Request httpclient, get httprestponse httpresponse HttpResponse = Httpclient.execute (HttpRequest); if (Httpresponse.getstatusline (). Getstatuscode () = = HTTPSTATUS.SC_OK) {//Get relevant information get httpentiy httpentity http
        Entity = Httpresponse.getentity ();
        InputStream is = Httpentity.getcontent ();
        Bitmap = Bitmapfactory.decodestream (IS);
      Is.close ();
      }else{Toast.maketext (context, "Connection failed!", Toast.length_short). Show ();
    } catch (Clientprotocolexception e) {e.printstacktrace ();
    catch (IOException e) {e.printstacktrace ();
return bitmap; }

For more information on Android-related content readers can view the site: "Android graphics and image processing skills summary", "Android Development introduction and Advanced Course", "Android debugging techniques and common problems solution summary", " Android Multimedia How-to Summary (audio, video, audio, etc), summary of Android Basic components usage, Android View tips Summary, Android layout layout tips and a summary of Android controls usage

I hope this article will help you with the Android program.

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.