Android之擷取本地圖片並壓縮方法

來源:互聯網
上載者:User

標籤:des   android   style   blog   http   io   ar   color   os   

首先既然要選擇圖片,我們就先要擷取本地所有的圖片,Android已經為我們封裝好了該意圖。

1 Intent intent = new Intent(Intent.ACTION_PICK, null);//從列表中選擇某項並返回所有資料2 intent.setDataAndType(3                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI,//得到系統所有的圖片4                    "image/*");//圖片的類型,image/*為所有類型圖片5 startActivityForResult(intent, PHOTO_GALLERY);

然後我們重寫onActivityResult方法。

在Android1.5後系統會調用MediaScanner服務進行後台掃描,索引歌曲,圖片,視頻等資訊,並將資料儲存在android.provider.MediaStore.Images.Thumbnails 和android.provider.MediaStore.Video.Thumbnails這兩個資料庫中。

所以我們需要使用Activity.managedQuery(uri, projection, selection, selectionArgs, sortOrder)方法從資料中擷取相應資料。

uri:  需要返回的資源索引

projection: 用於標識有哪些資料需要包含在返回資料中。

selection: 作為查詢合格過濾參數,類似於SQL語句中Where之後的條件判斷。

selectionArgs: 同上。

sortOrder: 對返回資訊進行排序。

 1      @Override 2      protected void onActivityResult(int requestCode, int resultCode, Intent data) 3      { 4          switch (requestCode)  5          { 6          //請求為擷取本地圖品時 7          case PHOTO_GALLERY: 8          {  9                 //圖片資訊需包含在返回資料中10                 String[] proj ={MediaStore.Images.Media.DATA};11                 //擷取包含所需資料的Cursor對象                  12                 @SuppressWarnings("deprecation")13                 Cursor cursor = managedQuery(data.getData(), proj, null, null, null); 14                 //擷取索引15                 int photocolumn =  cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);16                 //將游標一直開頭17                 cursor.moveToFirst();18                 //根據索引值擷取圖片路徑19                 String path = cursor.getString(photocolumn);20 21 22              break;23          }24          25          default:26              break;27          }

以上,我們便可取得本地圖片路徑了,接下來我們隊圖片進行壓縮處理。

 1       //先將所選圖片轉化為流的形式,path所得到的圖片路徑 2       FileInputStream is = new  FileInputStream(path); 3       //定義一個file,為壓縮後的圖片 4       File f = new File("圖片儲存路徑","圖片名稱"); 5       int size = " "; 6       Options options = new Options(); 7       options.inSampleSize = size; 8       //將圖片縮小為原來的  1/size ,不然圖片很大時會報記憶體溢出錯誤 9       Bitmap image = BitmapFactory.decodeStream(inputStream,null,options);10 11      is.close();12 13      ByteArrayOutputStream baos = new ByteArrayOutputStream();                14      image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//這裡100表示不壓縮,將不壓縮的資料存放到baos中15      int per = 100;                16      while (baos.toByteArray().length / 1024 > 500) { // 迴圈判斷如果壓縮後圖片是否大於500kb,大於繼續壓縮17      baos.reset();// 重設baos即清空baos18      image.compress(Bitmap.CompressFormat.JPEG, per, baos);// 將圖片壓縮為原來的(100-per)%,把壓縮後的資料存放到baos中19      per -= 10;// 每次都減少1020                     21      }22       //回收圖片,清理記憶體23      if(image != null && !image.isRecycled()){24          image.recycle();25          image = null;26          System.gc();27          }28      ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把壓縮後的資料baos存放到ByteArrayInputStream中29      btout.close();30      FileOutputStream os;31      os = new FileOutputStream(f);32      //自訂工具類,將輸入資料流複製到輸出資料流中33      StreamTransferUtils.CopyStream(btinput, os);34      btinput.close();35      os.close();

完成以後,我們可以在指定的圖片儲存路徑下看到壓縮的圖片。

Android之擷取本地圖片並壓縮方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.