//选择照片
Intent iphoto = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
iphoto.setType(“image/*”);
startActivityForResult(iphoto, Constant.REQUEST_SELECTPHOTO);
//拍照
Intent iPNow = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(iPNow, Constant.REQUEST_TAKEPHOTO);
//onActivityResult
//diaryPhoto 照片路径
// 照片
case Constant.REQUEST_SELECTPHOTO:
diaryPhoto= getSelectMediaPath(data);
setMediaPhoto();
break;
case Constant.REQUEST_TAKEPHOTO:
Bitmap bm = (Bitmap) data.getExtras().get("data");
if (bm != null) {
diaryPhoto = ImageUtils.savePicture(bm, TABKEPHOTO_PATH);
}
setMediaPhoto();
break;
//获取选择图片路径
private String getSelectMediaPath(Intent data){
String path ="";
Uri uri = data.getData(); // 获取别选中图片的uri
if (uri.toString().contains("file:///")) {
path = uri.toString().replace("file:///", "");
} else {
String[] filePathColumn = { MediaStore.Images.Media.DATA }; // 获取图库图片路径
Cursor cursor = getContentResolver().query(uri,
filePathColumn, null, null, null); // 查询选中图片路径
cursor.moveToFirst();
path = cursor.getString(cursor
.getColumnIndex(filePathColumn[0]));
cursor.close();
}
return path;
}
/**
* 保存图片
* */
public static String savePicture(Bitmap bm,String path){
File file2 = null;
if(SDCardUtil.isExsit()){
File file = new File(path);
if(!file.exists()){
file.mkdirs();
}
file2 = new File(file,System.currentTimeMillis()+".jpg");
FileOutputStream fos;
try {
fos = new FileOutputStream(file2);
if(bm != null){
bm.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
return file2.getAbsolutePath();
}
//获取清晰图片防止oom
String path= getSelectMediaPath(data);
if (!TextUtils.isEmpty(path)) {
try{
WindowManager wm = this.getWindowManager();
Display display = wm.getDefaultDisplay();
int wWidth = display.getWidth();
int wHeight = display.getHeight();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
options.inSampleSize = 1;
if(options.outWidth > wWidth || options.outHeight > wHeight){
options.inSampleSize = options.outWidth / wWidth;
if(options.inSampleSize < options.outHeight / wHeight)
options.inSampleSize = options.outHeight / wHeight;
}
options.inJustDecodeBounds = false;
photoBmp = BitmapFactory.decodeFile(path, options);
}catch(OutOfMemoryError oom){
return;
}
setDefinedSkin(photoBmp);
}`这里写代码片`