Now most of the network applications have a personal center of the module, there is no user upload image function, just recently in the project to use this from the Internet to find an integration, write a very good code for everyone to promote.
Needless to say, the code has been uploaded to the network disk, the need for friends can go to download to see, here to deal with the issue of uploading.
Asynchronous upload using Asynctask
Private classRegheadasynctaskextendsAsynctask<string, Void, string> { protected voidOnPreExecute () {}protectedbaseresponse doinbackground (String ... params) {List<NameValuePair> Postparams =NewArraylist<namevaluepair>(); Postparams.add (NewBasicnamevaluepair ("UID", UID); Postparams.add (NewBasicnamevaluepair ("image", image)); String Response=NULL; Try{String result=httpclientutil.uploadimg (Httpconfig.url_gethead (), postparams); if(Textutils.isempty (result))return NULL; Gson Gson=NewGson (); //parse the upload image operation, the information returned by the server ... } Catch(Exception e) {e.printstacktrace (); } returnresponse; } protected voidOnPostExecute (String result) {if(NULL!=result) { //information returned by the server ... if(flag) {//Upload SuccessfulImageloader.getinstance (). DisplayImage (ImageUrl, ImageView);//upload successfully updated UI } } }}
Post by using HTTP (Httpclientutil Class)
Public StaticString uploadimg (string URL, list<namevaluepair>namevaluepairs) {Basichttpparams Httpparams=NewBasichttpparams (); Httpconnectionparams.setconnectiontimeout (Httpparams,10 * 1000); Httpconnectionparams.setsotimeout (Httpparams,10 * 1000); HttpClient HttpClient=Newdefaulthttpclient (Httpparams); HttpPost HttpPost=Newhttppost (URL); String result= ""; Try{multipartentity entity=Newmultipartentity (httpmultipartmode.browser_compatible); for(intindex = 0; Index < namevaluepairs.size (); index++) { if(Namevaluepairs.get (Index). GetName (). Equalsignorecase ("image") {Entity.addpart (Namevaluepairs.get (index). GetName (),NewFilebody (NewFile (Namevaluepairs.get (index). GetValue ()), "image/*")); } Else{Entity.addpart (Namevaluepairs.get (index). GetName (),Newstringbody (Namevaluepairs.get (index). GetValue ())); }} httppost.setentity (entity); HttpResponse Response=Httpclient.execute (HttpPost); if(Response.getstatusline (). Getstatuscode () = = 200) {result=entityutils.tostring (Response.getentity ()); returnresult; } } Catch(IOException e) {e.printstacktrace (); } return NULL;}
Image upload PHP server (thinkphp)
//image Upload Public functionupimg () {$uptypes=Array( ' Image/jpg ', ' image/jpeg ', ' image/png ', ' imagepeg ', ' image/gif ', ' image/bmp ', ' image/x-png ' ); $user _id=$_post[' UID ']; $file=$_files["Image"]; $fname=$_files["Image"] ["Name"]; $fname _array=Explode(‘.‘,$fname); $extend=$fname _array[Count($fname _array)-1]; $MAX _file_size= 1024000; $dest _folder= "./uploads/avatar/";; if($extend!=""){ if(!In_array($file["Type"],$uptypes) {jsonerror ("Upload only picture files"); } if($file["Size"]>$MAX _file_size) {Jsonerror ("Avatar size cannot exceed 1M"); } $uploadfile=$dest _folder.$user _id.‘.‘.jpg; if(Move_uploaded_file($_files["Image"] ["Tmp_name"],$uploadfile)){ $user _info=m (' Users '); $data[' Headimg ']=$uploadfile; $result=$user _info->where ("uid=$user _id")->save ($data); if($result===FALSE) {Jsonerror ("Avatar modification Failed"); }Else{jsonsuccess ("Avatar modified successfully"); } }Else{jsonerror ("Picture upload Failed"); }} //processing avatar upload Data Public functionUpimgs () {//decide if you want to upload your membership if(!Empty($_files[' Image '] [' Name '])){ $target _path= "./uploads/avatar/";//Receive file directory $filename=$_files[' Image '] [' Name '];//file name $exename=$this->getexename ($filename);//Get extension $filename=$_post[' App_uid ']. '. '. jpg//Renaming a file $target _path=$target _path.$filename; if(Move_uploaded_file($_files[' Image '] [' Tmp_name '],$target _path)){ $user _id=$_post[' App_uid ']; $user _info=m (' Users '); $data[' Headimg ']=$filename; $result=$user _info->where ("uid=$user _id")->save ($data); if($result===FALSE) {Jsonerror ("Avatar modification Failed"); }Else{jsonsuccess ("Avatar Modified successfully!" "); } }Else{jsonerror ("There was an error uploading the picture, please try again"); } }}/** * Get file name extensions*/ Public functionGetexename ($file){ $arrName=Explode(‘.‘,$file); return End($arrName);}
Attach Photo Clipping demo http://pan.baidu.com/s/1hqIfdS4
Right-hand Letter contact Http://pan.baidu.com/s/1tUKmM
Personal Center module-photo clip upload