Android custom Dynamic Layout-Multi-Image Upload
Android custom Dynamic Layout-Multi-Image Upload
This article describes how to add images and upload multiple images in a Dynamic Layout in Android.
Project:
Technical points:
1.Dynamically Add the line in the pattern and add the + number of the image
2.Asynchronous upload of multiple images
First, let's take a look at the layout file:
The layout is very simple, mainly because a LinearLayout with id layout_container serves as the parent layout.
The horizontal line and vertical line layout are also very simple:
The following describes how to dynamically generate a layout:
Private void initUI () {setContentView (R. layout. activity_main); // setTitle (R. string. button_service_upload_picture); // showBackwardView (R. string. button_backward, true); // showForwardView (R. string. button_upload, true); // top-level parent layout mLayout = (ViewGroup) findViewById (R. id. layout_container); final int count = 9; // 9 grid final int rowCount = (count + 2)/3; for (int I = 0; I <rowCount; I ++) {if (I! = 0) {// load the Lateral Layout line View. inflate (this, R. layout. layout_line_horizonal, mLayout);} // create the layout object and set the final LinearLayout linearLayout = new LinearLayout (this); linearLayout. setBackgroundResource (R. drawable. row_selector); for (int j = 0; j <3; j ++) {if (j! = 0) {// load the View of the vertical layout of the inner layer. inflate (this, R. layout. layout_line_vertical, linearLayout);} ImageButton imageButton = new ImageButton (this); imageButton. setBackgroundResource (R. drawable. row_selector); imageButton. setTag (TAG); imageButton. setOnClickListener (this); imageButton. setEnabled (false); LinearLayout. layoutParams layoutParams = new LinearLayout. layoutParams (LayoutParams. MATCH_PARENT, LayoutParams. MATCH_PARENT, 1.0f); // Add it to the linearLayout layout. addView (imageButton, layoutParams); // Add the imageButton object to the mImageButtonList. add (imageButton);} DisplayManager manager = DisplayManager. getInstance (); LayoutParams layoutParams = new LayoutParams (LayoutParams. MATCH_PARENT, manager. dipToPixel (100); // Add the View to the total parent layout mLayout. addView (linearLayout, layoutParams);} // set the final ImageButton attribute for the outer layer to final ImageButton currentImageButton = mImageButtonList. get (mCurrent); currentImageButton. setImageResource (R. drawable. ic_add_picture); currentImageButton. setScaleType (ScaleType. CENTER); currentImageButton. setEnabled (true );}
Image Upload function:
Private class UploadPictureTask extends AsyncTask
, Integer, String> {/* (non-Javadoc) * @ see android. OS. AsyncTask # doInBackground (Params []) */@ Override protected String doInBackground (List
... Params) {final List
PictureList = params [0]; for (int I = 0, len = pictureList. size (); I <len; I ++) {final File file = new File (pictureList. get (I); // final String response = ApacheHttpUtils. post (mUrlPrefix +/upload, new File [] {file}); // resolution, storage // final UploadInfo upload = new UploadParser (). parse (response ). getData ();/* if (upload! = Null) {final String url = upload. getUrl (); if (url! = Null) {mPictureUrlList. add (url) ;}} */publishProgress (I);} return null;}/* (non-Javadoc) * @ see android. OS. asyncTask # onProgressUpdate (Progress []) */@ Override protected void onProgressUpdate (Integer... values) {}/* (non-Javadoc) * @ see android. OS. asyncTask # onPostExecute (java. lang. object) */@ Override protected void onPostExecute (String result) {// addPictures (); super. onPostExecute (result );}}
Note: The class declares three lists to save the records of previous operations.
mImageButtonList = new ArrayList
(); mPicturePathList = new ArrayList
(); mPictureUrlList = new ArrayList
();