Android An example of uploading a picture, including how to terminate the upload process, if the upload time to update the progress bar (a)

Source: Internet
Author: User

First on:



Layout is:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:background=" @drawable/ Black "android:orientation=" vertical "> <relativelayout xmlns:android=" http://schemas.android.com/apk/res/      Android "Android:layout_width=" Fill_parent "android:layout_height=" Fill_parent "android:layout_weight=" 1 " Android:isscrollcontainer= "true" > <imageview android:layout_width= "fill_parent" android:layout_height= "fill _parent "android:scaletype=" Centerinside "android:id=" @+id/photo_upload_preview_image "Android:layout_ Centerinparent= "true" > </ImageView> <!--Photo_upload_progress_item's location is the same as photo_upload_preview_image. , in order to show that Photo_upload_preview_image is still loading---<linearlayout xmlns:android= "http://schemas.android.com/apk/ Res/android "android:layout_width=" Wrap_content "Android:layout_height= "Wrap_content" android:paddingtop= "10DP" android:paddingbottom= "10DP" android:orientation= "Horizontal" Android:id= "@+id/photo_upload_progress_item" android:layout_centerinparent= "true" android:visibility= "Gone" > <progressbar android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_ma rginright= "4DP" android:layout_gravity= "center_vertical" android:visibility= "visible" style= "? Android : Attr/progressbarstylesmall "android:indeterminatedrawable=" @drawable/progress_bar_drawable "></        progressbar> <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content"        Android:layout_gravity= "center_vertical" android:gravity= "center" android:textcolor= "@drawable/white" android:text= "Loading photos ..." > </TextView> </LinearLayout> </RelativeLayout> <!--photo_ Upload_progress_layout in the Photo_upload_preview_image, with a progress bar, showing photo_Upload_preview_image Upload Progress--<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" Android Oid:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:id= "@+id/photo_upload_progress_ Layout "android:orientation=" vertical "android:background=" @drawable/black "android:visibility=" Gone "> < LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Fill_parent" an droid:layout_height= "10DP" android:background= "@drawable/upload_photo_progress_bg" android:layout_margintop= "6DP "Android:layout_marginbottom=" 6DP "android:layout_marginleft=" 6DP "android:layout_marginright=" 6DP "a Ndroid:layout_gravity= "Center_horizontal" android:paddingleft= "0DP" android:paddingright= "0DP" android:id= " @+id/photo_upload_progress_bar_container "> <view android:layout_width=" 3DP "android:layout_height = "Fill_parent" android:id= "@+id/photo_Upload_progress_bar "android:background=" @drawable/upload_photo_progress_bar "> </view></linearlayo Ut><textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_marg Inbottom= "6DP" android:layout_gravity= "center_horizontal" android:text= "photo crosses ..." android:textcolor= "@drawa Ble/white "></TextView> </LinearLayout> <linearlayout xmlns:android=" http://schemas.android.com/ Apk/res/android "android:layout_width=" fill_parent "android:layout_height=" wrap_content "android:id=" @+id/photo_ Upload_bottom_bar "android:orientation=" vertical "android:background=" #BFBFBF "android:paddingleft=" 10DP "Android: paddingright= "10DP" android:paddingbottom= "4DP" android:paddingtop= "2DP" > <relativelayout xmlns:android= "htt  P://schemas.android.com/apk/res/android "android:layout_width=" fill_parent "android:layout_height=" Wrap_content " Android:id= "@+id/photo_upload_album_laYout "android:paddingtop=" 5DP "android:background=" #BFBFBF "> <textview xmlns:android="/http/  Schemas.android.com/apk/res/android "android:layout_width=" wrap_content "android:layout_height=" Wrap_content " android:layout_marginleft= "5DP" android:text= "uploaded to" android:textsize= "16DP" android:singleline= "true" Android:  Textcolor= "@drawable/gray2" android:gravity= "center_vertical" android:id= "@+id/photo_upload_ablum_prompt" > </TextView> <textview xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "  Wrap_content "android:layout_height=" wrap_content "android:layout_torightof=" @id/photo_upload_ablum_prompt " android:layout_marginleft= "5DP" android:text= "mobile album" android:textsize= "16DP" android:singleline= "true" Android: Gravity= "center_vertical" android:textcolor= "@drawable/black" android:id= "@+id/photo_upload_ablum_name" > </ Textview> <!--on the right, you can select albums--<button android:id= "@+id/photo_upload_choose_album"        Android:layout_width= "Wrap_content" android:layout_height= "40DP" android:layout_alignparentright= "tr UE "android:text=" album "Android:textcolor=" @drawable/black "android:paddingright=" 30DP "Android: background= "@drawable/location_button" > </Button> </RelativeLayout> <!--this in photo_upload_album_ Below layout Add module--<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" for position android:i D= "@+id/photo_upload_location_layout" android:layout_below= "@id/photo_upload_album_layout" android:layout_width= "Fill_parent" android:layout_height= "wrap_content" > <relativelayout xmlns:android= "Http://schemas.androi     D.com/apk/res/android "android:id=" @+id/photo_upload_location_right_layout "android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:layout_alignparentright= "true" android:background= "#BFBFBF" and roid:paddingtop= "5DP" Androidoid:paddingbottom= "5DP" > <button android:id= "@+id/photo_upload_add_gps" android:layout_width= "wrap _content "android:layout_height=" 40DP "android:text=" position "android:textcolor=" @drawable/black "a ndroid:paddingright= "30DP" android:background= "@drawable/location_button" > </Button> </relativelay out> <relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:id= "@+id/photo_uplo Ad_location_left_layout "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:layo ut_marginright= "5DP" android:layout_alignparentleft= "true" android:layout_toleftof= "@id/photo_upload_location_        Right_layout "> <imageview android:layout_width=" wrap_content "android:layout_height=" Wrap_content " android:layout_marginleft= "5DP" android:layout_marginright= "10DP" android:id= "@+id/photo_upload_locatio N_icon "Android:src="@drawable/location "> </ImageView> <button android:id=" @+id/photo_upload_remove_loc ation "android:layout_width=" wrap_content "android:layout_height=" 40DP "android:text=" Removal "Android Oid:textcolor= "@drawable/black" android:background= "@drawable/common_button_style" Android:layout_alignparen      Tright= "true" android:layout_marginleft= "5DP" android:layout_margintop= "5DP" > </Button> <textview android:id= "@+id/photo_upload_location_des" android:layout_width= "Wrap_content" android:l ayout_height= "Wrap_content" android:textcolor= "@drawable/black" android:layout_margintop= "10DP" Andro id:layout_toleftof= "@id/photo_upload_remove_location" android:layout_torightof= "@id/photo_upload_location_icon" > </TextView> </RelativeLayout> </RelativeLayout><!--This edittext user can enter some information, just below the location module -<edittext andRoid:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:id= "@+id/photo_upload_title" a Ndroid:singleline= "true" Android:maxlength= "android:maxlines=" 3 "android:hint=" Please enter: "Android:input Type= "Textmultiline" > </EditText> <!--These two buttons are just below EditText and <linearlayout xmlns:android = "Http://schemas.android.com/apk/res/android" android:layout_width= "fill_parent" android:layout_height= "Wrap_ Content "> <button android:layout_width=" wrap_content "android:layout_height=" Wrap_content "android:layou t_marginright= "15DP" android:background= "@drawable/common_button_style" android:text= "Upload" android:id= "@+id/photo_    Upload_button_upload "android:textcolor=" @drawable/black "android:layout_weight=" 1 "android:textsize=" 18DP "> </Button> <button android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android: background= "@drawable/common_buTton_style "android:text=" cancels "android:id=" @+id/photo_upload_button_cancel "android:textcolor=" @drawable/black "   android:layout_weight= "1" android:textsize= "18DP" > </Button> </LinearLayout> </LinearLayout> </LinearLayout>

Here we take a picture of the local SD root directory, turn it into bitmap, and then save it to the Tmp folder to upload:

Private class Loadphototask extends Asynctask<void, Void, boolean>{private Bitmap mloadedbitmap = null; Protected Boolean doinbackground (Void ... params) {try {if (mfilepathname.length () > 0) {File File = new file (mfilepat Hname); if (!file.exists ()) {return false;} Bitmapfactory.options opt = new Bitmapfactory.options (), Long fileSize = File.length (); int maxSize = 2*1024 * 1024;IF (FileS ize <= maxSize) {opt.insamplesize = 1;} else if (fileSize <= maxSize * 4) {<strong>//less than 8m</strong>opt.insamplesize = 2;} Else{long times = filesize/maxsize;opt.insamplesize = (int) (Math.log (Times)/Math.log (2.0)) + 1; <strong>//math.l OG returns the base e logarithm </strong>}try{mloadedbitmap = Bitmapfactory.decodefile (mfilepathname,opt); <strong>// Opt for reduced multiples </strong>muploadfilepathname = Savebitmaptofile (Mloadedbitmap);} catch (OutOfMemoryError e) {toast.maketext (Uploadphotoactivity.this, Getresources (). getString (R.string.no_memory_ To_view_photo), Toast.length_short). Show (); UploadPhotoActivity.this.finish ();}} return true;}    catch (Exception e) {log.e ("uploadphotoactivity", "Doinbackground", e); return false;}  } protected void OnPostExecute (Boolean result) {try {Showloadpreviewprogressbar (false); if (mloadedbitmap! = NULL) {ImageView Iamgepreview = (ImageView) Findviewbyid (r.id.photo_upload_preview_image); I Amgepreview.setimagebitmap (MLOADEDBITMAP);} Else{}mloadedbitmap = null;}    catch (Exception e) {log.e ("uploadphotoactivity", "OnPostExecute", e); }    }

Private String Savebitmaptofile (Bitmap bmp) {if (null = = BMP) {return null;} String fileName = "upload_tmp.jpg"; <strong>file f = this.getfilestreampath (filename); </strong>//data/ Data/com.example.tianqitongtest/files/upload_tmp.jpg, this is the location of the file to be uploaded if (f.exists ()) {F.delete ();} FileOutputStream ostream;try {<strong>int Targetwidth = 780;int w = bmp.getwidth (); if (W > targetwidth) {int h = Bmp.getheight (); int targetheight = (Targetwidth * h)/w;bmp = Bitmap.createscaledbitmap (BMP, Targetwidth, targetHeight,t Rue);//Generate a new Bitmap}</strong>ostream = This.openfileoutput (FileName, mode_private) According to the specified width and height; bmp.compress ( Bitmap.CompressFormat.JPEG, ostream); Ostream.flush (); Ostream.close (); ostream = null;} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();} return F.getabsolutepath ();}

You need to put the keyboard back when uploading:

private void Hideinputmethod () {View view = Getcurrentfocus (); if (View! = null) {    ((Inputmethodmanager) Getsystemservice (Input_method_service)). Hidesoftinputfromwindow (View.getwindowtoken (), InputMethodManager.HIDE_ not_always);}}

Get the 11.jpg picture path on the SD card:

Private String Getstoredpicpath () {String fileName = "11.jpg"; return environment.getexternalstoragedirectory () + "/" + FileName;}


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.