拍照
//拍照
private void takephoto(){
//定一个拍照图片的路径
photopath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/lmp/pic/"
+ LmpUntil.gettime() + ".png";
if (Build.VERSION.SDK_INT >= 24 ) {
//兼容android7.0 使用共享文件的形式
ContentValues contentValues = new ContentValues(1);
contentValues.put(MediaStore.Images.Media.DATA, photopath);
//定一个Uri
output = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
}else{
output = Uri.fromFile(new File(photopath));
}
Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, output);
startActivityForResult(openCameraIntent, TAKE_PHOTO);
}
从系统相册中选择
//调用系统图片选择
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, 33 );
onActivityResult中的操作
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TAKE_PHOTO && resultCode == RESULT_OK) {
//注释部分是直接获取拍照后的bitmap
// Bitmap bitmap = null;
// if (data != null) {
// if (data.hasExtra("data")) {
// Log.i("URI", "data is not null");
// bitmap = data.getParcelableExtra("data");
// headphoto.setImageBitmap(bitmap);//imageView即为当前页面需要展示照片的控件,可替换
// }
// } else {
// Log.i("URI", "Data is null");
// if (Build.VERSION.SDK_INT >= 24){
//
// try {
// bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(output));
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// }
// headphoto.setImageBitmap(bitmap);
// }else {
// bitmap = BitmapFactory.decodeFile(photopath);
// headphoto.setImageBitmap(bitmap);
// }
// }
//剪切
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(output, "image/*");
intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, output);
startActivityForResult(intent, CROP_PHOTO); // 启动裁剪程序
}else if (requestCode == CROP_PHOTO&& resultCode == RESULT_OK) {
//裁剪完成将bitmap保存至目标位置
try {
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(output));
headphoto.setImageBitmap(bitmap);
File fileorg = new File(photopath);
if(fileorg.exists()){
fileorg.delete();
}
photocrappath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/lmp/pic/headcrap.png";
File file = new File(photocrappath);
FileOutputStream out = null;
out = new FileOutputStream(file);
if (bitmap != null) {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
} else {
throw new FileNotFoundException();
}
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else if ( requestCode == 33 ){ //系统图片选择之后的回调
if (data != null) {
Uri uri = data.getData();
try {
Bitmap mBitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
// Bitmap mBitmap = MediaStore.Images.Media.getBitmap(cr, uri);//显得到bitmap图片
headphoto.setImageBitmap(mBitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
剪裁后可以根据uri获取文件路径 拿到路径后可以做个压缩处理下面提供根据uri获取文件路径的方法
// 从Uri中获取文件路径
public static String getpathfromuri( Uri uri ,Context context){
final String scheme = uri.getScheme();
String lujing = null;
if ( scheme == null )
lujing = uri.getPath();
else if ( ContentResolver.SCHEME_FILE.equals( scheme ) ) {
lujing = uri.getPath();
} else if ( ContentResolver.SCHEME_CONTENT.equals( scheme ) ) {
Cursor cursor = context.getContentResolver().query( uri, new String[] { MediaStore.Images.ImageColumns.DATA }, null, null, null );
if ( null != cursor ) {
if ( cursor.moveToFirst() ) {
int index = cursor.getColumnIndex( MediaStore.Images.ImageColumns.DATA );
if ( index > -1 ) {
lujing = cursor.getString( index );
}
}
cursor.close();
}
}
return lujing;
}