Android: An Example of uploading images (1)

Source: Internet
Author: User

Android: An Example of uploading images (1)

First:



Layout is:

 
     
      
       
           
       
   
        
        
        
     
      
    
    
         
          
    
   
     
        
    
     
      
      
      
      
      
        
       
     
        
         
            
      
         
          
               
               
          
          
          
   
     
      
        
      
       
            
          
     
    
 

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
 
  
{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 ){
  // Less than 8 MOpt. inSampleSize = 2;} else {long times = fileSize/maxSize; opt. inSampleSize = (int) (Math. log (times)/Math. log (2.0) + 1;
  // Math. log returns the base-e logarithm.} Try {mLoadedBitmap = BitmapFactory. decodeFile (mFilePathName, opt );
  // Opt is a zoom-in multipleMUploadFilePathName = 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 {sho WLoadPreviewProgressBar (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 ";File f = this. getFileStreamPath (fileName );// 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 {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}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.