Android Imitation micro-letter said to achieve photo-and-mapping upload function _android

Source: Internet
Author: User

The example of this article for everyone to share the Android imitation micro-letter said, mood functions for your reference, the specific contents are as follows

Not only can realize the photo, select the gallery, the case of multiple patterns upload, the current many apps have similar functions of micro-letter friends, can be published to say such as the accompanying picture upload. The following is the process of implementing this feature: You have not seen the Android Retrofit 2.0 frame Upload Image Solution This article, looking at today's very easy, and then in this project to use a library:photopicker, Encapsulates the selection function of the picture, whether the camera is selected, and the ability to view the picture when the picture is selected.

One, first: will photopicker to the project
(1), first of all, briefly explain the use of photopickerintent:

Photopickerintent Intent = new Photopickerintent (mainactivity.this); 
  Intent.setselectmodel (Selectmodel.multi); 
  Intent.setshowcarema (TRUE);//whether 
  to 
  display the photo intent.setmaxtotal (6); Intent.setselectedpaths (imagepaths); The selected photo address, which is used to echo the selected state 
  Startactivityforresult (Intent, Request_camera_code); 

(2), after setting, rewrite the Onactivityresult method to process the selected picture and preview load adapter

@Override 
 protected void onactivityresult (int requestcode, int resultcode, Intent data) { 
 Super.onactivityresult (Requestcode, ResultCode, data); 
 if (ResultCode = = RESULT_OK) { 
  switch (requestcode) { 
  //select photo Case 
  Request_camera_code: 
   loadadpater ( Data.getstringarraylistextra (Photopickeractivity.extra_result)); 
   break; 
  Preview Case 
  Request_preview_code: 
   loadadpater (Data.getstringarraylistextra (Photopreviewactivity.extra_ result)); 
   break; 
  }}} 
  

Second, focus on Gridadapter

(1), define a variable mmaxposition,
(2), write a method to get the maximum position: to get the maximum position of the item.

 The focus here is when position==mmaxposition-1 lets "+" Select the picture's default picture display. 
 //When currently up to 6 pictures, then Max 7 o'clock, picture shadow hidden. 
 if (position==mmaxposition-1) { 
 holder.image.setImageResource (r.mipmap.ic_launcher); 
 Holder.image.setVisibility (view.visible); 
 if (position==6&&mmaxposition==7) { 
 holder.image.setImageResource (r.mipmap.ic_launcher); 
 Holder.image.setVisibility (View.gone); 
 } 
 else { 
 final String path=listurls.get (position); 
 Glide.with (mainactivity.this). 
  Load (new File (path)) 
  . Placeholder (r.mipmap.default_error). 
  Error ( R.mipmap.default_error) 
  . Centercrop (). Crossfade (). into 
  (holder.image); 
 

(3), about the display of pictures:

 Private class Gridadapter extends baseadapter{private arraylist<string> listurls; 
 private int mmaxposition; 
 Private Layoutinflater Inflater; 
  Public Gridadapter (arraylist<string> listurls) {this.listurls = Listurls; 
 Inflater = Layoutinflater.from (mainactivity.this); 
  public int GetCount () {mmaxposition = Listurls.size () +1; 
 return mmaxposition; 
 public int getmaxposition () {return mmaxposition; 
 @Override public String getitem (int position) {return listurls.get (position); 
 @Override public long getitemid (int position) {return position; 
  @Override public View getview (int position, View Convertview, ViewGroup parent) {Viewholder holder = null; 
  if (Convertview = = null) {holder = new Viewholder (); 
  Convertview = Inflater.inflate (R.layout.item_image, Parent,false); 
  Holder.image = (ImageView) Convertview.findviewbyid (R.id.imageview); 
  Convertview.settag (holder); else {holder = (viewholder) Convertview.gettag (); 
  } log.d ("", "Position:" +position+ "mmaxposition:" +mmaxposition); 
  The focus here is when position==mmaxposition-1 lets "+" Select the picture's default picture display. 
  When currently up to 6 pictures, then Max 7 o'clock, picture shadow hidden. 
  if (position==mmaxposition-1) {//Holder.image.setTag ("Default"); 
  Holder.image.setImageResource (R.mipmap.ic_launcher); 
  Holder.image.setVisibility (view.visible); 
   if (position==6&&mmaxposition==7) {holder.image.setImageResource (r.mipmap.ic_launcher); 
  Holder.image.setVisibility (View.gone); 
  } else {final String path=listurls.get (position); Glide.with (mainactivity.this). Load (new File (path)). Placeholder (r.mipmap.default_error). Error (R.mipmap.defau 
  Lt_error). Centercrop (). Crossfade (). into (Holder.image); 
 }

III. Click event of item in the GridView

Gridview.setonitemclicklistener (New Adapterview.onitemclicklistener () { 
  @Override public 
  void Onitemclick ( Adapterview<?> Parent, view view, int position, long id) { 
 
 
   if (position = = Gridadapter.getmaxposition ()-1) {
   photopickerintent Intent = new Photopickerintent (mainactivity.this); 
   Intent.setselectmodel (Selectmodel.multi); 
   Intent.setshowcarema (TRUE); 
   Startactivityforresult (Intent, Request_camera_code); 
   } else{ 
    photopreviewintent Intent = new Photopreviewintent (mainactivity.this); 
    Intent.setcurrentitem (position); 
    Intent.setphotopaths (imagepaths); 
    Startactivityforresult (Intent, Request_preview_code); 
   } 
  } 
  ); 

Overall Source:

Package com.lidong.photopickersample; 
Import android.content.Intent; 
Import Android.os.Bundle; 
Import android.support.v7.app.AppCompatActivity; 
Import Android.util.Log; 
Import Android.view.LayoutInflater; 
Import Android.view.View; 
Import Android.view.ViewGroup; 
Import Android.widget.AdapterView; 
Import Android.widget.BaseAdapter; 
Import Android.widget.Button; 
Import Android.widget.EditText; 
Import Android.widget.GridView; 
Import Android.widget.ImageView; 
Import Com.bumptech.glide.Glide; 
Import Com.lidong.photopicker.ImageCaptureManager; 
Import com.lidong.photopicker.PhotoPickerActivity; 
Import com.lidong.photopicker.PhotoPreviewActivity; 
Import Com.lidong.photopicker.SelectModel; 
Import com.lidong.photopicker.intent.PhotoPickerIntent; 
Import com.lidong.photopicker.intent.PhotoPreviewIntent; 
Import Org.json.JSONArray; 
Import Java.io.File; 
 
 
Import java.util.ArrayList; /** * @ * @author Lidong * @date 2016-02-29/public class Mainactivity extends appcompatactivity {private static final int request_camera_code = 10; 
 private static final int request_preview_code = 20; 
 Private arraylist<string> imagepaths = new arraylist<> (); Private Imagecapturemanager Capturemanager; 
 Camera photo Processing class private GridView GridView; 
 private int columnWidth; 
 Private Gridadapter Gridadapter; 
 Private Button Mbutton; 
 Private String Depp; 
 
 
 Private EditText TextView; 
 @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
 Setcontentview (R.layout.activity_main); 
 GridView = (GridView) Findviewbyid (R.id.gridview); 
 Mbutton = (Button) Findviewbyid (R.id.button); 
 textview= (EditText) Findviewbyid (R.id.et_context); 
 int cols = Getresources (). Getdisplaymetrics (). Widthpixels/getresources (). Getdisplaymetrics (). densitydpi; cols = cols < 3? 
 3:cols; 
 Gridview.setnumcolumns (cols); Preview Gridview.setonitemclicklistener (new Adapterview.onitemclicklistener () {@OVerride public void Onitemclick (adapterview<?> parent, view view, int position, long id) {if (position = g 
  Ridadapter.getmaxposition ()-1) {photopickerintent intent = new Photopickerintent (mainactivity.this); 
  Intent.setselectmodel (Selectmodel.multi); Intent.setshowcarema (TRUE); Whether to show photo intent.setmaxtotal (6); Select the maximum number of photos, the default is 9 intent.setselectedpaths (imagepaths); 
 The selected photo address, which is used to echo the selected state Startactivityforresult (intent, Request_camera_code); 
  }else{photopreviewintent Intent = new Photopreviewintent (mainactivity.this); 
  Intent.setcurrentitem (position); 
  Intent.setphotopaths (imagepaths); 
 Startactivityforresult (Intent, Request_preview_code); 
 } 
 } 
 }); 
 Gridadapter = new Gridadapter (imagepaths); 
 
 
 Gridview.setadapter (Gridadapter); Mbutton.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {Depp =textview.gette 
 XT (). ToString (). Trim ()!=null?textview.gettext (). ToString (). Trim (): "Woowoeo"; New ThreaD () {@Override public void run () {super.run (); 
  Fileuploadmanager.upload (Imagepaths,depp); 
 }}.start (); 
 } 
 }); @Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {Super.onactivityresu 
 Lt (Requestcode, ResultCode, data); if (ResultCode = = RESULT_OK) {switch (Requestcode) {//Select photo Case Request_camera_code:loadadpater (data.getstring 
  Arraylistextra (Photopickeractivity.extra_result)); 
 Break 
  Preview Case Request_preview_code:loadadpater (Data.getstringarraylistextra (Photopreviewactivity.extra_result)); 
 Break }} private void Loadadpater (arraylist<string> paths) {if (imagepaths = = null) {imagepaths = new Arra 
 Ylist<> (); 
 } imagepaths.clear (); 
 Imagepaths.addall (paths); 
 try{Jsonarray obj = new Jsonarray (imagepaths); 
 LOG.E ("--", obj.tostring ()); 
 }catch (Exception e) {e.printstacktrace (); 
 } gridadapter.notifydatasetchanged (); } Private Class GridadApter extends baseadapter{private arraylist<string> listurls; 
 private int mmaxposition; 
 Private Layoutinflater Inflater; 
 Public Gridadapter (arraylist<string> listurls) {this.listurls = Listurls; 
 Inflater = Layoutinflater.from (mainactivity.this); 
 public int GetCount () {if (listurls.size () = = 9) {mmaxposition = Listurls.size () +1; 
 else {mmaxposition = Listurls.size () +1; 
 return mmaxposition; 
 public int getmaxposition () {return mmaxposition; 
 @Override public String getitem (int position) {return listurls.get (position); 
 @Override public long getitemid (int position) {return position; 
 @Override public View getview (int position, View Convertview, ViewGroup parent) {Viewholder holder = null; 
 
 
 if (Convertview = = null) {holder = new Viewholder (); 
 Convertview = Inflater.inflate (R.layout.item_image, Parent,false); 
 Holder.image = (ImageView) Convertview.findviewbyid (R.id.imageview); COnvertview.settag (holder); 
 else {holder = (Viewholder) convertview.gettag (); 
 
 
 } log.d ("", "Position:" +position+ "mmaxposition:" +mmaxposition); 
 if (position==mmaxposition-1) {//Holder.image.setTag ("Default"); 
 Holder.image.setImageResource (R.mipmap.ic_launcher); 
 Holder.image.setVisibility (view.visible); 
  if (position==6&&mmaxposition==7) {holder.image.setImageResource (r.mipmap.ic_launcher); 
 Holder.image.setVisibility (View.gone); 
 } else {final String path=listurls.get (position); Glide.with (mainactivity.this). Load (new File (path)). Placeholder (r.mipmap.default_error). Error (R.mipmap.default_ 
 Error). Centercrop (). Crossfade (). into (Holder.image); 
 return convertview; 
 public class Viewholder {public ImageView image; 

 } 
 } 
}

Uploaded classes: Fileuploadmanager.java

Package com.lidong.photopickersample; 
Import Android.util.Log; 
Import Com.squareup.okhttp.MediaType; 
Import Com.squareup.okhttp.RequestBody; 
Import Java.io.File; 
Import java.util.ArrayList; Import Retrofit. 
Call; Import Retrofit. 
Callback; Import Retrofit. 
Gsonconverterfactory; Import Retrofit. 
Response; Import Retrofit. 
Retrofit; 
Import Retrofit.http.Multipart; 
Import Retrofit.http.POST; 
 
 
Import Retrofit.http.Part;  /** * Created by Lidong on 2016/2/29 */public class Fileuploadmanager {private static final String ENDPOINT = 
 
 
 "http://192.168.1.122:8080";  
 Public interface Fileuploadservice {/** * upload a picture * @param description * @param imgs * @return * * @Multipart @POST ("/upload") call<string> Uploadimage (@Part ("FileName") String description, @Part ("file\"; filename=\ "I 
 
 
 
 
 Mage.png\ "") Requestbody IMGs);  /** * Upload 6 Pictures * @param description * @param imgs1 * @param imgs2 * @param imgs3 * @param imgs4 * @param imgs5
 * @param imgs6 * @return/@Multipart @POST ("/upload") call<string> Uploadimage (@Part ("description") St Ring description, @Part ("file\"; filename=\ "image.png\") Requestbody imgs1, @Part ("file\"; filename=\ "image.png\" ") Requestbody Imgs2, @Part (" file\ "); Filename=\ "image.png\") Requestbody Imgs3, @Part ("file\"; filename=\ "image.png\") Requestbody Imgs4, @Part ("File \"; 
 Filename=\ "image.png\") Requestbody imgs5, @Part ("file\"; filename=\ "image.png\") requestbody IMGS6); Private static final Retrofit Sretrofit = new retrofit. Builder (). BaseURL (ENDPOINT). Addconverterfactory (Gsonconverterfactory.create ())//. Addcalladapterfactory ( 
 
 
 Rxjavacalladapterfactory.create ())///Use Rxjava as callback adapter. Build (); 
 
 private static final Fileuploadservice Apimanager = sretrofit.create (Fileuploadservice.class); 
 /** * said * @param paths * @param desp/public static void upload (arraylist<string> paths,string desp) { RequestbOdy[] requestbody= new requestbody[6]; if (Paths.size () >0) {for (int i=0;i<paths.size (); i++) {Requestbody[i] = Requestbody.create (Mediatype.parse ( 
 "Multipart/form-data"), New File (Paths.get (i))); } call<string> call = Apimanager.uploadimage (desp,requestbody[0],requestbody[1],requestbody[2],requestbody[ 
 3],REQUESTBODY[4],REQUESTBODY[5]);  Call.enqueue (New callback<string> () {@Override public void Onresponse (response<string> Response, retrofit 
 Retrofit) {LOG.V ("Upload", Response.message ()); 
 LOG.V ("Upload", "success"); 
 @Override public void OnFailure (Throwable t) {log.e ("Upload", t.tostring ()); 
 
 
 } 
 }); 
 } 
}

Effect:

This article has been organized into the "Android micro-credit Development tutorial Summary," Welcome to learn to read.

The above is the entire content of this article, I hope to help you 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.