// Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// startActivityForResult(camera, CAMERA);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
String sdStatus = Environment.getExternalStorageState();
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
Log.i("DoubleScreenCameraTestFile", "SD card is not avaiable/writeable right now.");
return;
}
//获取相机返回的数据
Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");// 转换为Bitmap图片格式
//获取系统当前时间 yy-mm-dd
Date date=new Date();
SimpleDateFormat format=new SimpleDateFormat("yyyyMMdd");
String time=format.format(date);
System.out.println(time);
try {
// 使用IO流将照片写入指定文件
String DCIMPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString();
final String DIRECTORY = DCIMPath + "/Camera";
file = new File(DIRECTORY,"/IMG_"+time+"_"+".jpg");
output = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, output);// 把数据写入文件
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//更新媒体库
MediaScannerConnection.scanFile(getApplicationContext(), new String[] { file.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener(){
@Override
public void onScanCompleted(
String path, Uri uri) {
// TODO Auto-generated method stub
}
} );
}