现在的位置: 首页 > 综合 > 正文

android 拍照指定存储路径 检查SD卡状态

2013年07月05日 ⁄ 综合 ⁄ 共 1116字 ⁄ 字号 评论关闭
private void takePhoto() {
	if (Environment.getExternalStorageState().equals(
			Environment.MEDIA_MOUNTED)) {
		Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
		String directoryPath =Constants.FILE_SAVE_PATH+"/Photo/Bills";//Constants.FILE_SAVE_PATH为常量字符串
		File fileDirectory = new File(directoryPath);
		if (!fileDirectory.exists()) {
			fileDirectory.mkdirs();
		}
		try {
			mPhotoPath = directoryPath+"/"+getPhotoFileName();
			mPhotoFile = new File(mPhotoPath);
			if(!mPhotoFile.exists()){
				mPhotoFile.createNewFile();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Toast.makeText(SendCostPhoto.this, "No such file or directory",
					Toast.LENGTH_LONG).show();
		}
		intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mPhotoFile));
		startActivityForResult(intent, REQUEST_CAMERA);
	} else {
		Toast.makeText(SendCostPhoto.this, R.string.sdcarderror, Toast.LENGTH_LONG).show();
	}
}
private String getPhotoFileName() {
	Date date = new Date(System.currentTimeMillis());
	SimpleDateFormat dateFormat = new SimpleDateFormat(
			"'IMG'_yyyyMMdd_HHmmss_SSS");
	return dateFormat.format(date)+".jpg";
}

注意:存储目录必须先创建文件存储的目录(fileDirectory.mkdirs())否则在mPhotoFile.createNewFile()时抛IOException: No such file or directory

抱歉!评论已关闭.