设置以时间命名的文件路径及名称

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值