android項目中的拍照和本地圖片截圖

來源:互聯網
上載者:User

android項目中的拍照和本地圖片

1,得到儲存檔案的地址

private static final String IMAGE_FILE_LOCATION = Environment.getExternalStorageDirectory()+"/temp.jpg";private Uri imageUri;imageUri = Uri.fromFile(new File(IMAGE_FILE_LOCATION));


2,相簿和拍照按鈕的點擊事件

                // 相簿imageViewLocalPhoto.setOnClickListener(new OnClickListener() {public void onClick(View v) {Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,//content://media/external/images/mediaIMAGE_UNSPECIFIED);startActivityForResult(intent, PHOTOZOOM);}});// 拍照imageViewTakePhoto.setOnClickListener(new OnClickListener() {public void onClick(View v) {Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "temp.jpg")));System.out.println("============="+ Environment.getExternalStorageDirectory());startActivityForResult(intent, PHOTOHRAPH);}});

3,在onActivityResult回調方法中

拍照和本地圖片傳入的uri不同

 // 拍照 顯示圖片頁面if (requestCode == PHOTOHRAPH) {startPhotoZoom(imageUri);}// 讀取相簿縮放圖片 顯示圖片頁面if (requestCode == PHOTOZOOM) {startPhotoZoom(data.getData());}

4,方法

public void startPhotoZoom(Uri uri, int flag) {Intent intent = new Intent("com.android.camera.action.CROP");intent.setDataAndType(uri, "image/*");intent.putExtra("crop", "true");intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//映像輸出intent.putExtra("outputFormat",Bitmap.CompressFormat.JPEG.toString());intent.putExtra("noFaceDetection", true);intent.putExtra("return-data", false);//回調方法data.getExtras().getParcelable("data")返回資料為空白startActivityForResult(intent, PHOTORESOULT);}


5,回調方法返回結果

@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {    // 處理結果 處理縮放過後的圖片    if (requestCode == PHOTORESOULT) {                flag_layout = "layout2";Bundle extras = data.getExtras();if (extras != null) {    photo = extras.getParcelable("data");}            if(photo==null)//載入本地            {            Bitmap bm = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/temp.jpg");            imageView.setImageBitmap(bm);            }            else//載入接收的圖片資料            {            imageView.setImageBitmap(photo);            }  }  super.onActivityResult(requestCode, resultCode, data);}

傳統的方法為:1,不加這句intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//映像輸出,,

2,這句改為返回資料為true:intent.putExtra("return-data", true);

3,也就沒有剪下後的輸出圖片,通過data.getExtras().getParcelable("data")接收返回的映像資料

說明:經實驗,傳統的方法經常死機,報綁定資料過大的錯誤,所以改用本例的方法比較好用了,就是截好圖片後,直接載入本地圖片就好了

bug:如果刪除temp.jpg檔案後,拍照時會一直截刪掉的temp.jpg檔案,本地圖片沒問題,再換回拍照也沒問題了



聯繫我們

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