Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android一個上傳圖片的例子(一)

android一個上傳圖片的例子(一)

編輯:關於Android編程

先上效果圖:

\


Layout為:



  
  
  	  
  	  
  	  
  	  
  	  
		
		
    	
    	
  	  
  
  
  
  
  	
      
      
	
	
	
  
  
  
  
	
  	
  		
  		
  		
  		
  		
  		
  	
  	
  	
  	
     
	     
	  
  	
     
     
     
     
     
     
     
      
	
	
  	
   	
  	
  	 
	 
  	
  	  
  	  
  	
  
  


這裡我們是把本地SD根目錄下的一張圖片,轉成Bitmap,再存到要上傳的tmp文件夾裡面:

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){ //小於8M
						opt.inSampleSize = 2;
					}else{
						long times = fileSize / maxSize;
						opt.inSampleSize = (int)(Math.log(times) / Math.log(2.0)) + 1; //Math.log返回以e為底的對數
					}
					try{
						mLoadedBitmap = BitmapFactory.decodeFile(mFilePathName,opt);//opt為縮小的倍數
						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);
					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/data/com.example.tianqitongtest/files/upload_tmp.jpg,這個是要上傳的文件存的位置
		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);//根據指定寬度和高度來生成一個新的Bitmap
			}
			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 block
			e.printStackTrace();
		}
		return f.getAbsolutePath();
	}

上傳的時候需要把鍵盤收起:

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

得到SD卡上的11.jpg的圖片路徑:

private String getStoredPicPath() {
		String fileName = "11.jpg";
		return Environment.getExternalStorageDirectory()+"/"+fileName;
	}
	


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved