天天看點

解決通過Intent調用系統拍照程式,傳回圖檔太小的問題[android]

以下的代碼可以調用系統的拍照程式,

1

2

intent it = newintent("android.media.action.image_capture");

startactivityforresult(it, activity.default_keys_dialer);

按下拍照鍵後,會傳回到你的activity,是以你的activity要在onactivityresult方法裡加一個處理,

3

4

5

6

7

8

9

10

11

12

13

protectedvoidonactivityresult(intrequestcode, intresultcode, intent data) {

     super.onactivityresult(requestcode, resultcode, data);

     try{

         bundle extras = data.getextras();

         bitmap b = (bitmap) extras.get("data");

         take = b;

         imageview img = (imageview)findviewbyid(r.id.image);

         img.setimagebitmap(take);

     }catch(exception e){

     }

}

但是這樣你會發現這個bitmap尺寸太小了。明顯是被壓縮過了,要像傳回未被壓縮的照片,那麼你要給調用系統拍照程式intent加上參數,指定圖檔輸出的位置。

it.putextra(mediastore.extra_output, uri.fromfile(newfile(f.sd_card_temp_photo_path)));

這樣就是大圖檔傳回了。

         take = u.resizebitmap(u.getbitmapforfile(f.sd_card_temp_photo_path), 640);

         imgflag = true;

另外注意一下,傳回的那個bitmap會很大,你用完以後要把它回收掉,不然你很容易記憶體報oom錯誤

14

15

16

17

publicstaticbitmap resizebitmap(bitmap bitmap, intnewwidth) {

     intwidth = bitmap.getwidth();

     intheight = bitmap.getheight();

     floattemp = ((float) height) / ((float) width);

     intnewheight = (int) ((newwidth) * temp);

     floatscalewidth = ((float) newwidth) / width;

     floatscaleheight = ((float) newheight) / height;

     matrix matrix = newmatrix();

     // resize the bit map

     matrix.postscale(scalewidth, scaleheight);

     // matrix.postrotate(45);

     bitmap resizedbitmap = bitmap.createbitmap(bitmap, 0, 0, width, height, matrix, true);

     bitmap.recycle();

     returnresizedbitmap;

繼續閱讀