Skip to content

Instantly share code, notes, and snippets.

@iamcxa
Created November 9, 2016 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamcxa/3692e6ba26e817ad6b42c5d6b7d3713f to your computer and use it in GitHub Desktop.
Save iamcxa/3692e6ba26e817ad6b42c5d6b7d3713f to your computer and use it in GitHub Desktop.
/** Create a File for saving an image or video */
private File getOutputMediaFile(){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
// p.s: MODIFY THE PATH YOU WANT YOUR IMAGE TO STORE HERE
// 嗨嗨如果你想改儲存路徑記得改這邊
// for example, you can just using this way to save image into the root of sdcard
// File mediaStorageDir = new File(Environment.getExternalStorageDirectory().toString());
File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
+ "/Android/data/"
+ getApplicationContext().getPackageName()
+ "/Files");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(new Date());
File mediaFile;
String mImageName="MI_"+ timeStamp +".jpg";
mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName);
return mediaFile;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment