Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 圖片上傳功能實作:相機、相簿與多圖選擇器

編輯:關於 Android 程式設計

最近在開發使用者意見回饋功能,需要讓使用者輸入文字或提交圖片,效果大致類似微信朋友圈那樣的多圖選擇器。

一開始我找了一個用 universal-image-loader 寫的現成方案,實作很快,但容易發生記憶體溢出,而且不易處理。實在沒轍,只好自己查資料動手寫。

先說明整體思路:進入選擇圖片的頁面用 GridView 呈現;點擊「新增」圖示時用 Dialog 彈出選單,並為 Dialog 加上進場動畫;進入圖片瀏覽頁同樣用 GridView;點擊「所有圖片」時則用 Dialog 搭配 ListView 呈現。以下是相應程式碼。

 private void showDialog() { View view = getLayoutInflater().inflate(R.layout.user_header_dialog, null); final Dialog dialog = new Dialog(this, R.style.transparentFrameWindowStyle); dialog.setContentView(view, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); Window window = dialog.getWindow(); // 設置顯示動畫 window.setWindowAnimations(R.style.main_menu_animstyle); WindowManager.LayoutParams wl = window.getAttributes(); wl.x = 0; wl.y = getWindowManager().getDefaultDisplay().getHeight(); // 以下這兩句是為了保證按鈕可以水平滿屏 wl.width = ViewGroup.LayoutParams.MATCH_PARENT; wl.height = ViewGroup.LayoutParams.WRAP_CONTENT; // 設置顯示位置 dialog.onWindowAttributesChanged(wl); // 設置點擊外圍解散 dialog.setCanceledOnTouchOutside(true); dialog.show(); btn_picture = (Button) window.findViewById(R.id.btn_picture); btn_photo = (Button) window.findViewById(R.id.btn_photo); btn_cancle = (Button) window.findViewById(R.id.btn_cancle); btn_picture.setOnClickListener(new View.OnClickListener() {// 圖庫 @SuppressLint("InlinedApi") @Override public void onClick(View v) { Intent intent = new Intent(PhotoSelectActivity.this, AlbumActivity.class); startActivity(intent); dialog.dismiss(); } }); btn_photo.setOnClickListener(new View.OnClickListener() {// 相機 @SuppressLint("InlinedApi") @Override public void onClick(View v) { photo(); dialog.dismiss(); } }); btn_cancle.setOnClickListener(new View.OnClickListener() {// 取消 @Override public void onClick(View v) { dialog.dismiss(); } }); } 

這是彈出選單的部分。這裡要特別留意 Android 6.0 的權限差異:6.0 以下版本只要在 AndroidManifest.xml 中宣告即可;6.0 及以上除了宣告,還必須在執行期動態申請權限,否則呼叫相機或存取 SD 卡時會直接閃退。本例尚未對 6.0 以上做適配,相關做法我會在後續文章中說明。

 protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case TAKE_PICTURE: if (Bimp.tempSelectBitmap.size() < 9 && resultCode == RESULT_OK) { File file = new File(Environment.getExternalStorageDirectory() + "/" + mImageFileName); mImagePath = file.getPath(); Bitmap bitmapFromUrl = FileUtils.getBitmapFromUrl(mImagePath, 320, 480); String[] split = mImagePath.split("0/"); String strUrl = ""; if (split != null && split.length > 0) { strUrl = split[1]; } // 重新緩存圖片 FileUtils.setPicToView(PhotoSelectActivity.this,bitmapFromUrl, strUrl); // 獲取重新緩存圖片的大小 File iconDir = FileUtils.getIconDir(PhotoSelectActivity.this); String absolutePath = iconDir.getAbsolutePath(); String picPath = absolutePath + strUrl; ImageItem takePhoto = new ImageItem(); takePhoto.setBitmap(bitmapFromUrl); takePhoto.setImagePath(picPath); Bimp.tempSelectBitmap.add(takePhoto); } break; } } 

這段是拍照返回後的回呼:取得圖片的同時進行壓縮,並把結果快取到 SD 卡中,再取回相應路徑。這裡同時用 size() < 9 限制了最多九張,與朋友圈的規格一致。

 /** * 清空圖片集合 */ private void cleanImageList() { Bimp.max = 0; Bimp.tempSelectBitmap.clear(); } 

在點擊返回或按下實體返回鍵時,要記得把定義的靜態變數歸零,並清空保存圖片用的靜態 list 集合,否則下次進入時會殘留上次的選擇結果。

 private void initPow() { View view = getLayoutInflater().inflate(R.layout.listview_popupwindows, null); final Dialog dialog = new Dialog(this, R.style.Dialog_Fullscreen); dialog.setContentView(view, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); Window window = dialog.getWindow(); // 設置顯示動畫 window.setWindowAnimations(R.style.main_menu_animstyle); WindowManager.LayoutParams wl = window.getAttributes(); wl.x = 0; wl.y = getWindowManager().getDefaultDisplay().getHeight(); int height = 0; int h=(int) (mScreenHeight / 1.6); int listH=AlbumActivity.contentList.size()*DensityUtil.dip2px(AlbumActivity.this,80); if (listH==0) { height=h; }else{ if (listH>h) { height=h; }else{ height=listH; } } // 以下這兩句是為了保證按鈕可以水平滿屏 wl.width = ViewGroup.LayoutParams.MATCH_PARENT; wl.height = height; // 設置顯示位置 dialog.onWindowAttributesChanged(wl); // 設置點擊外圍解散 dialog.setCanceledOnTouchOutside(true); dialog.show(); ListView listview = (ListView) window.findViewById(R.id.listview); ListAdapter listAdapter = new ListAdapter(AlbumActivity.this); listview.setAdapter(listAdapter); listview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { dataList = (ArrayList<ImageItem>) AlbumActivity.contentList.get(arg2).imageList; String folderName = AlbumActivity.contentList.get(arg2).bucketName; tv_all.setText("" + folderName); gridImageAdapter = new AlbumGridViewAdapter(AlbumActivity.this, dataList, Bimp.tempSelectBitmap); agridView.setAdapter(gridImageAdapter); dialog.dismiss(); } }); } 

這段是圖片選擇頁中點擊「所有圖片」時的彈窗,用 Dialog 加 ListView 實作。此處要特別注意高度計算:先算出所有 ListView 條目高度總和,再取螢幕高度;若條目總高超過螢幕高度的 1.6 倍,就採用螢幕高度/1.6,否則採用條目總高。這樣處理之後,彈窗高度就能自適應不同資料量。

運行效果如下:

原始碼:Androidphoto

以上就是本文的全部內容,希望對你的開發有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved