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