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里
	}
}




 

展开阅读全文

没有更多推荐了,返回首页