1,通过intent调用camera
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); File photo = new File(Environment.getExternalStorageDirectory(), getPhotoFileName()); Log.i(TAG,"getPhotoFileName():" +getPhotoFileName()); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo)); imageUri = Uri.fromFile(photo); startActivityForResult(intent, GET_PHOTO_WITH_CAMERA);private String getPhotoFileName() { Date date = new Date(System.currentTimeMillis()); SimpleDateFormat dateFormat = new SimpleDateFormat( "'IMG'_yyyy-MM-dd_HH-mm-ss"); return dateFormat.format(date) + ".jpg"; }
然后onActivityResult()中
if (requestCode == GET_PHOTO_WITH_CAMERA) { // doCropPhoto(mCurrentPhotoFile); Uri selectedImage = imageUri; Log.i(TAG, "uri:" + imageUri.toString()); getContentResolver().notifyChange(selectedImage, null); ContentResolver cr = getContentResolver(); Bitmap bitmap; try { bitmap = android.provider.MediaStore.Images.Media .getBitmap(cr, selectedImage); BitmapDrawable bd = new BitmapDrawable(bitmap); headerImage.setBackgroundDrawable(bd); } catch (Exception e) { Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT) .show(); } }
2,如果要调用gallery去剪辑的话
doCropPhoto(photo)
protected void doCropPhoto(File f) { try { // 启动gallery去剪辑这个照片 final Intent intent = getCropImageIntent(Uri.fromFile(f)); startActivityForResult(intent, GET_PHOTO_WITH_GALLARY); } catch (Exception e) { } } public static Intent getCropImageIntent(Uri photoUri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(photoUri, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", 80); intent.putExtra("outputY", 80); intent.putExtra("return-data", true); return intent; }