There are a lot of ways to implement photo uploads on the web, and if you run it with a new version of Android, you may find it impossible to achieve it. The main reason is that Android started with the 4.4 version of the selector through Intent.action_get_content, and the URI returned by GetData does not contain the actual file path, but rather like this "content:// Com.android.providers.media.documents/document/image:1234 ", so that the traditional way to find the path of the picture. The simplest solution is to use intent. Action_pick instead of intent.action_get_content.
The following gives the 4.4 version of the upload after the implementation of the specific methods:
First step: Click the Picture button code
Click on the photo
Btnheadcamera.setonclicklistener (new View.onclicklistener () {
@Override public
void OnClick ( View view) {
Intent itcamera=new Intent (mediastore.action_image_capture);
Startactivityforresult (itcamera,0);
}
);
Step Two: Save photo Picture code
@Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {switch (Requestcode) {
Case 0://Photo Savephoto (data);
Break
} super.onactivityresult (Requestcode, ResultCode, data); Final String save_path=environment.getexternalstoragedirectory () + "/my_head.jpg";
Save the path after taking a picture//save pictures public void Savephoto (Intent it) {Bundle bundle=it.getextras ();
if (bundle!=null) {Bitmap photo = bundle.getparcelable ("Data");
Imghead.setimagebitmap (photo);
File Filehead=new file (Save_path); try {if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {if (!filehead.getparen
Tfile (). exists ()) {Filehead.getparentfile (). mkdir ();
} bufferedoutputstream bos=new Bufferedoutputstream (New FileOutputStream (Filehead));
Photo.compress (Bitmap.compressformat.jpeg,80,bos);
Bos.flush ();
Bos.close ();
}else {Toast Toast = Toast.maketext (Headphotoactivity.this, save failed!)
", Toast.length_short);
Toast.setgravity (gravity.center, 0, 0);
Toast.show ();
}}catch (FileNotFoundException e) {e.printstacktrace ();
}catch (IOException e) {e.printstacktrace ();
}
}
}
Step three: upload picture code
String Server_url = config.photoapi+ "/uploadimage"; Uploaded server-side API address Btnheadcancel.setonclicklistener (new View.onclicklistener () {@Override public void OnClick (View VI EW) {New Thread (new Runnable () {@Override public void Run () {File File = new Fi
Le (Save_path);
msg = new Message ();
Msg.what = 0;
if (file!=null) {try {int re = imageutils.uploadform (file, Server_url);
Msg.obj = re;
catch (IOException ex) {msg.obj = 0;
Toast.maketext (Headphotoactivity.this, "Upload failed", Toast.length_short). Show ();
Handler.sendmessage (msg);
}else {toast.maketext (headphotoactivity.this, "Can't find upload Picture", Toast.length_short). Show ();
}}). Start ();
}
});
final Handler handler=new Handler () {@Override public void Handlemessage (Mes Sage msg) {switch (msg.what) {case 0:if (int) msg.obj = 1) {Toast.maketex
T (headphotoactivity.this, "Upload success", Toast.length_short). Show ();
else {toast.maketext (headphotoactivity.this, "Upload failed", Toast.length_short). Show ();
} break;
}
}
};
/** * * * @param uploadfile * Upload files * @param serverurl * Uploaded server path * @throws IOException *
/public static int Uploadform (File uploadfile, String ServerURL) throws IOException {int re=0;
String fileName = Uploadfile.getname ();
StringBuilder sb = new StringBuilder ();
Sb.append ("--" + boundary + "\ r \ n"); Sb.append ("Content-disposition:form-data; Name=\ "" + fileName + "\";
Filename=\ "" + fileName + "\" + "\ r \ n");
Sb.append ("content-type:image/jpeg" + "\ r \ n");
Sb.append ("\ r \ n");
byte[] HeaderInfo = sb.tostring (). GetBytes ("UTF-8");
Byte[] Endinfo = ("\r\n--" + boundary + "--\r\n"). GetBytes ("UTF-8");
System.out.println (Sb.tostring ());
URL url = new URL (serverurl);
HttpURLConnection conn = (httpurlconnection) url.openconnection ();
Conn.setrequestmethod ("POST"); Conn.setrequestproperty ("Content-type", "multipart/form-data;
boundary= "+ boundary); Conn.setrequestproperty ("ConTent-length ", String. valueof (Headerinfo.length + uploadfile.length () + endinfo.length));
Conn.setdooutput (TRUE);
OutputStream out = Conn.getoutputstream ();
InputStream in = new FileInputStream (uploadfile);
Out.write (HeaderInfo);
byte[] buf = new byte[1024];
int Len;
while (len = In.read (buf))!=-1) out.write (buf, 0, Len);
Out.write (Endinfo);
In.close ();
Out.close ();
if (conn.getresponsecode () = =) {re=1;
return to re;
}
The
Finally gives the service-side WEBAPI code:  
[HttpPost] public async task
Overwrite upload filename public
class Custommultipartformdatastreamprovider:multipartformdatastreamprovider
{
Public Custommultipartformdatastreamprovider (String path)
: Base (Path)
{} public
override string Getlocalfilename (System.Net.Http.Headers.HttpContentHeaders Headers)
{
string fileName = DateTime.Now.ToString ("Yyyymmddhhmmssfff");
Return FileName + "_" + headers. ContentDisposition.FileName.Replace ("\"), String. Empty);//base. Getlocalfilename (headers);
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.