1.
private String getOutputMediaPath() {
java.util.Date date = new java.util.Date();
String timeTemp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(date.getTime());
//File storageFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator +
"VID_" + timeTemp + ".mp4");
Log.e("Error", "getOutputMediaPath file path---"+file.getAbsolutePath());
return file.getAbsolutePath();
}
2.
private String getOutputMediaPath() {
java.util.Date date = new java.util.Date();
String timeTemp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(date.getTime());
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
Log.e("Error", "getOutputMediaPath file path---"+
mediaStorageDir.getPath() + File.separator +
"VID_" + timeTemp+ ".mp4");
return mediaStorageDir.getPath() + File.separator +
"VID_" + timeTemp + ".mp4";
}
Saving Media Files(谷歌官方)
Media files created by users such as pictures and videos should be saved to a device's externalstorage directory (SD Card) to conserve system space and to allow users to access these fileswithout their device. There are many possible directory locations to save media files on a device,however there are only two standard locations you should consider as a developer:
Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_PICTURES
) - This method returns the standard, shared and recommendedlocation for saving pictures and videos. This directory is shared (public), so other applicationscan easily discover, read, change and delete files saved in this location. If your application isuninstalled by the user, media files saved to this location will not be removed. To avoidinterfering with users existing pictures and videos, you should create a sub-directory for yourapplication's media files within this directory, as shown in the code sample below. This method isavailable in Android 2.2 (API Level 8), for equivalent calls in earlier API versions, seeSaving Shared Files.Context.getExternalFilesDir
(Environment.DIRECTORY_PICTURES
) - This method returns a standard location for savingpictures and videos which are associated with your application. If your application is uninstalled,any files saved in this location are removed. Security is not enforced for files in thislocation and other applications may read, change and delete them.
The following example code demonstrates how to create a File
orUri
location for a media file that can be used when invoking a device's camera withanIntent
or as part of aBuilding a CameraApp.
public static final int MEDIA_TYPE_IMAGE = 1; public static final int MEDIA_TYPE_VIDEO = 2; /** Create a file Uri for saving an image or video */ private static Uri getOutputMediaFileUri(int type){ return Uri.fromFile(getOutputMediaFile(type)); } /** Create a File for saving an image or video */ private static File getOutputMediaFile(int type){ // To be safe, you should check that the SDCard is mounted // using Environment.getExternalStorageState() before doing this. File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES), "MyCameraApp"); // 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()){ Log.d("MyCameraApp", "failed to create directory"); return null; } } // Create a media file name String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); File mediaFile; if (type == MEDIA_TYPE_IMAGE){ mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_"+ timeStamp + ".jpg"); } else if(type == MEDIA_TYPE_VIDEO) { mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_"+ timeStamp + ".mp4"); } else { return null; } return mediaFile; }