微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android调用系统照相机拍照并自定义名称存储

1.调用照相机

//启动系统照相机
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
startActivityForResult(intent, 1);

2.拍照后存储

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

//选择存储
if (resultCode == Activity.RESULT_OK) {
String sdStatus = Environment.getExternalStorageState();
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
Log.v("TestFile",
"SD card is not avaiable/writeable right Now.");
Toast.makeText(this, "未检测到SD卡", Toast.LENGTH_SHORT).show();
return;
}

Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式
FileOutputStream b = null;

//取得当前日期时间
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");      
Date curDate = new Date(System.currentTimeMillis());//获取当前时间      
String strDate = formatter.format(curDate);
//文件名:车牌号+代码+时间
String fname = carId + photoKindCode + strDate + ".jsp";
String path = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
//String path = "/sdcard/myImage/" + fname;
path = path + "/myImage/" + fname;

try {
b = new FileOutputStream(path);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件
//insert一条照片记录
PhotoRecord photoRecord = new PhotoRecord();
photoRecord.setCarId(carId);
photoRecord.setPhotoKindCode(photoKindCode);
photoRecord.setPhotoName(fname);
photoRecord.setPhotoAddress(path);
photoRecord.setPhotoTime(strDate);
photoRecord.setUpload(false);
dataService.savePhotoRecord(photoRecord);
} catch (Exception e) {
e.printstacktrace();
} finally {
try {
b.flush();
b.close();
} catch (IOException e) {
e.printstacktrace();
}
}

((ImageView) findViewById(R.id.imageView)).setimageBitmap(bitmap);// 将图片显示在ImageView里
}
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐