private static final int PHOTO_REQUEST_TAKEPHOTO = 1;// 拍照 private static final int PHOTO_REQUEST_GALLERY = 2;// 從相冊中選擇 private static final int PHOTO_REQUEST_CUT = 3;// 結果 private File tempFile = new File(Environment.getExternalStorageDirectory(), getPhotoFileName()); protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case PHOTO_REQUEST_TAKEPHOTO:// 當選擇拍照時調用 startPhotoZoom(Uri.fromFile(tempFile)); break; case PHOTO_REQUEST_GALLERY:// 當選擇從本地獲取圖片時 // 做非空判斷,當我們覺得不滿意想重新剪裁的時候便不會報異常,下同 if (data != null) { System.out.println("11================"); startPhotoZoom(data.getData()); } else { System.out.println("================"); } break; case PHOTO_REQUEST_CUT:// 返回的結果 if (data != null) // setPicToView(data); sentPicToNext(data); break; } super.onActivityResult(requestCode, resultCode, data); }
// 使用系統當前日期加以調整作為照片的名稱 private String getPhotoFileName() { Date date = new Date(System.currentTimeMillis()); SimpleDateFormat dateFormat = new SimpleDateFormat( "'IMG'_yyyyMMdd_HHmmss"); return dateFormat.format(date) + ".jpg"; }
調用系統拍照功能:
Intent cameraintent = new Intent( MediaStore.ACTION_IMAGE_CAPTURE); // 指定調用相機拍照后照片的儲存路徑 cameraintent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile)); startActivityForResult(cameraintent, PHOTO_REQUEST_TAKEPHOTO);
調用系統相冊功能:
Intent getAlbum = new Intent(Intent.ACTION_GET_CONTENT); getAlbum.setType("image/*"); startActivityForResult(getAlbum, PHOTO_REQUEST_GALLERY);
調用系統裁剪功能:
private void startPhotoZoom(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); // crop為true是設置在開啟的intent中設置顯示的view可以剪裁 intent.putExtra("crop", "true"); // aspectX aspectY 是寬高的比例 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // outputX,outputY 是剪裁圖片的寬高 intent.putExtra("outputX", 300); intent.putExtra("outputY", 300); intent.putExtra("return-data", true); intent.putExtra("noFaceDetection", true); System.out.println("22================"); startActivityForResult(intent, PHOTO_REQUEST_CUT); }
自定義對話框:
mDialog = new AlertDialog.Builder(this, R.style.FullScreenDialog) .create(); if (mDialog != null && !mDialog.isShowing()) { mDialog.show(); mDialog.setContentView(R.layout.dialog_select_imge); mDialog.setCanceledOnTouchOutside(false);
}
自定義style :
<style name="FullScreenDialog" parent="android:style/Theme.Dialog"> <item name="android:windowNoTitle">true</item> <item name="android:windowFrame">@null</item> <item name="android:windowIsFloating">true</item> <item name="android:windowIsTranslucent">false</item> <item name="android:background">@android:color/transparent</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:backgroundDimEnabled">true</item> </style>