An example of how android uploads an image, including how to terminate the upload process. If the progress bar is updated during the upload Process (1 ),

Source: Internet
Author: User
Tags tmp folder

An example of how android uploads an image, including how to terminate the upload process. If the progress bar is updated during the upload Process (1 ),

First:



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: isScroll Container = "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> <! -- The location of the cursor is the same as photo_upload_preview_image, In order to display 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/resize" android: layout_cente RInParent = "true" android: visibility = "gone"> <ProgressBar android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_marginRight = "4dp" android: layout_gravity = "center_vertical" android: visibility = "visible" style = "? Android: attr/progressBarStyleSmall "android: indeterminateDrawable =" @ drawable/reply "> </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> <! -- Under photo_upload_preview_image, the progress bar is displayed, showing the upload progress of photo_upload_preview_image --> <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_progress_layout" android: orientation = "vertical" android: background = "@ drawable/black" android: visibility = "gone"> <Linear Layout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: 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" android: layout_gravity = "Copyright" android: paddingLeft = "0dp" android: padding Right = "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> </LinearLayout> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_marginBottom = "6dp" android: layout_gravity = "Center_horizontal" android: text = "uploading photos... "android: textColor =" @ drawable/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: paddingRig Ht = "10dp" android: paddingBottom = "4dp" android: paddingTop = "2dp"> <RelativeLayout xmlns: android = "http://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 = "upload 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 side, select album --> <Button android: id = "@ + id/photo_upload_choose_album" android: layout_width = "wrap_content" android: layout_height = "40dp" android: layout_alignParentRight = "true" android: text = "album" android: textColor = "@ drawable/black" android: paddingRight = "30dp" android: background = "@ drawable/location_button"> </Button> </RelativeLayout> <! -- In the photo_upload_album_layout to add a module for the location --> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: id = "@ + 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.android.com/apk/res/android" android: id = "@ + id/photo_upload_lo Cation_right_layout "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: Rule =" true "android: background =" # BFBFBF "android: paddingTop =" 5dp "android: paddingBottom = "5dp"> <Button android: id = "@ + id/photo_upload_add_gps" android: layout_width = "wrap_content" android: layout_height = "40dp" android: text = "location" android: textColor = "@ drawable/black" android: paddingRig Ht = "30dp" android: background = "@ drawable/location_button"> </Button> </RelativeLayout> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: id = "@ + id/HYPERLINK" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_marginRight = "5dp" android: layout_alignParentLeft = "true" android: layout_toLeftOf = "@ id/photo_upload_l Ocation_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_location_icon" android: src = "@ drawable/location"> </ImageView> <Button android: id = "@ + id/photo_upload_remove_location" android: layout_width = "wrap_content" android: layout_height = "40dp" android: text =" Remove "android: textColor =" @ drawable/black "android: background =" @ drawable/common_button_style "android: layout_alignParentRight =" true "android: layout_marginLeft =" 5dp "android: layout_marginTop = "5dp"> </Button> <TextView android: id = "@ + id/photo_upload_location_des" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: textColor = "@ drawable/black" android: layout_marginTop = "10dp" Android: layout_toLeftOf = "@ id/photo_upload_remove_location" android: layout_toRightOf = "@ id/Hangzhou"> </TextView> </RelativeLayout> <! -- The editText user can enter some information at the bottom of the location module --> <EditText android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: id = "@ + id/photo_upload_title" android: singleLine = "true" android: maxLength = "63" android: maxLines = "3" android: hint = "Enter: "android: inputType =" textMultiLine "> </EditText> <! -- The two buttons are at the bottom of editText --> <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: layout_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 = "cancel" android: id = "@ + id/photo_upload_button_cancel" android: textColor = "@ drawable/black" android: layout_weight = "1" android: textSize = "18dp"> </Button> </LinearLayout>

Here we convert an image under the Local SD root directory into Bitmap and save it to the tmp folder to be uploaded:

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 (mFilePathName); if (! File. exists () {return false;} BitmapFactory. options opt = new BitmapFactory. options (); long fileSize = file. length (); int maxSize = 2*1024*1024; if (fileSize <= maxSize) {opt. inSampleSize = 1;} else if (fileSize <= maxSize * 4) {<strong> // less than 8 M </strong> opt. inSampleSize = 2;} else {long times = fileSize/maxSize; opt. inSampleSize = (int) (Math. log (times)/Math. log (2.0) + 1; <strong> // Math. log returns the base-e logarithm. </str Ong >}try {mLoadedBitmap = BitmapFactory. decodeFile (mFilePathName, opt); <strong> // opt is a shrinking multiple </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 ("UploadPhotoActiv Ity "," 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); IamgePreView. 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/com. example. tianqitongtest/files/upload_tmp.jpg. This is the location where the file to be uploaded is stored 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, true); // generate a new Bitmap Based on the specified width and height} </strong> ostream = this. openFileOutput (fileName, MODE_PRIVATE); bmp. compress (Bitmap. compressFormat. JPEG, 70, ostream); ostream. flush (); ostream. close (); ostream = null;} catch (Exception e) {// TODO Auto-generated catch blocke. printStackTrace ();} return f. getAbsolutePath ();}

During upload, You need to collapse the keyboard:

private void hideInputMethod(){View view = getCurrentFocus();if(view != null){    ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);}}

The path to the image 11.jpg on the SD card is as follows:

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.