Android在程式中啟動拍照/攝像或選擇一張圖片/視頻,並製作縮圖後儲存

來源:互聯網
上載者:User

有時候我們需要選擇一個圖片來做頭像或是上傳到相簿中,這時候我們可以選擇啟動相機拍照,或者從圖庫中選擇。

首先看看如何啟動相機拍照並取得所拍的照片。

下面是啟動相機拍照並返回所拍的照片的代碼:

public void capturePicture(){    //啟動拍照,並儲存到臨時檔案    Intent intent = new Intent();    intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));    startActivityForResult(intent, REQUEST_CAPTURE_IMAGE);}

下面是啟動相機攝像並返回所拍的錄影的代碼:

public void captureVideo(){    Intent intent2 = new Intent();    intent2.setAction(MediaStore.ACTION_VIDEO_CAPTURE);    intent2.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0); //設定為低品質    startActivityForResult(intent2, REQUEST_CAPTURE_VIDEO);}

下面是開啟圖庫(既系統預設的圖片選取器),選擇一個圖片或視頻的代碼:

public void choosePicture(){    Intent intent3 = new Intent();    intent3.setType("image/*");    intent3.setAction(Intent.ACTION_GET_CONTENT);    startActivityForResult(intent3, REQUEST_CHOOSE);}

這樣子整個過程就完成了,拍完照後我們按“確定”健,或者點擊選擇了照片後,返回到了我們程式的介面,那如何取得剛才的照片呢?請注意上面的代碼中使用了startActivityForResult方法,所以很明顯我們需要重載onActivityResult方法來拿到返回的資料。代碼如下:

        @Override       protected void onActivityResult(int requestCode, int resultCode, Intent data) {       super.onActivityResult(requestCode, resultCode, data);       switch(resultCode){            case RESULT_OK:                        Uri uri = data.getData(); // 得到Uri                     String fPath = uri2filePath(uri); // 轉化為路徑                            /* 做你想做的處理 **/                            break;                }         }

整個過程到此完成。

下面附上製作縮圖和把Uri轉化為檔案路徑的方法!
製作圖片或視頻的縮圖:

        /** 建立縮圖,返回縮圖檔案路徑 */public String createThumbnail(Bitmap source, String fileName){int oldW = source.getWidth();int oldH = source.getHeight();int w = Math.round((float)oldW/MAX_SIZES);  //MAX_SIZE為縮圖最大尺寸int h = Math.round((float)oldH/MAX_SIZES);int newW = 0;int newH = 0;if(w <= 1 && h <= 1){return saveBitmap(source, fileName);}int i = w > h ? w : h;  //擷取縮放比例newW = oldW/i;newH = oldH/i;Bitmap imgThumb = ThumbnailUtils.extractThumbnail(source, newW, newH);  //關鍵代碼!!return saveBitmap(imgThumb, fileName);  //註:saveBitmap方法為儲存圖片並返迴路徑的private方法}/** 建立影片縮圖,返回縮圖檔案路徑 */public String createVideoThumbnail(String filePath, String fileName){Bitmap videoThumb = ThumbnailUtils.createVideoThumbnail(filePath, Thumbnails.MINI_KIND);  //關鍵代碼!!return saveBitmap(videoThumb, fileName);  //註:saveBitmap方法為儲存圖片並返迴路徑的private方法}

把Uri轉化為檔案路徑:

        /** 把Uri轉化成檔案路徑 */private String uri2filePath(Uri uri){String[] proj = { MediaStore.Images.Media.DATA };Cursor cursor = managedQuery(uri,proj,null,null,null);int index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);cursor.moveToFirst();String path = cursor.getString(index);return path;}

OK!打完收工!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.