/**
* 启动系统相机
*
* @param view
*/
public void takePhoto(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CASE_CAMERA);
}
拍完成照后的回调函数如下:
/**
* 拍照后的回调函数
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == CASE_CAMERA) {
Bundle extras = data.getExtras();
//获取图片信息
Bitmap bitmap = (Bitmap) extras.get("data");
//保存图片
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/tmp";
saveImage(bitmap,path);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
位图的保存:
/**
* 保存图片
* @param bitmap
* @param path 保存路径
* @throws IOException
*/
public void saveImage(Bitmap bitmap,String path) throws IOException{
String imgePath = "";
String imgeName = "img";
File filePath = new File(path);
if(!filePath.exists()){
filePath.mkdir();
}
int i = 0;
while(filePath.exists()){
imgePath = path+"/"+imgeName+(i++)+".jpg";
filePath = new File(imgePath);
}
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(filePath));
// 压缩位图到指定的OutputStream
bitmap.compress(Bitmap.CompressFormat.JPEG, 0, bos);
// 刷新此缓冲区的输出流
bos.flush();
// 关闭此输出流并释放与此流有关的所有系统资源
bos.close();
}