这话题老生常谈了,只是为了自己便于阅读,在这里记下来,如果你有类似经验,可以不看了。
创建图片保存的路径:
private final File PHOTO_DIR = new File(
Environment.getExternalStorageDirectory() + "/DCIM/Camera");
private File mCurrentPhotoFile;// 照相机拍照得到的图片
调用系统拍照:
PHOTO_DIR.mkdirs();// 创建照片的存储目录
mCurrentPhotoFile = new File(PHOTO_DIR,
getPhotoFileName());// 给新照的照片文件命名
Intent intt = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE,
null);
intt.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(mCurrentPhotoFile));
startActivityForResult(intt,
SlookConstant.CHANGEUSERICON);
/**
*
* 用当前时间给取得的图片命名
*/
private String getPhotoFileName() {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat dateFormat = new SimpleDateFormat(
"'IMG'_yyyy-MM-dd HH:mm:ss");
return dateFormat.format(date) + ".png";
}
接收拍照后返回的值:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case SlookConstant.CHANGEUSERICON:
Intent intent = getCropImageIntent(Uri.fromFile(mCurrentPhotoFile));
startActivityForResult(intent, ImageHelper.PHOTO_PICKED_WITH_DATA);
break;
case ImageHelper.PHOTO_PICKED_WITH_DATA:
/** 用来标识请求gallery的activity */
if (null != data) {
Bitmap photo = data.getParcelableExtra("data");
//这里的photo就是截取后返回的图片值;
}
break;
}
}
调用系统截剪图片的界面:
/**
*
* Constructs an intent for image cropping. 调用图片剪辑程序
*/
private 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", 84);
intent.putExtra("outputY", 84);
intent.putExtra("return-data", true);
return intent;
}
这里有几个常量,自己定义取值即可;