首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >设置自定义视频存储路径

设置自定义视频存储路径
EN

Stack Overflow用户
提问于 2014-05-08 17:01:07
回答 1查看 1K关注 0票数 0

我很难理解我要更改的代码的哪一部分来设置自定义的文件存储路径。此活动成功地打开了android摄像头应用程序,允许您拍摄视频,然后返回到“视频保存到:"File:///storage/emulated/0/videoapp/examplevideoname.mp4”“的活动中。

我只想把它保存到我选择的另一个文件夹,但这是来自google教程,我不知道我需要更改的部分:http://developer.android.com/guide/topics/media/camera.html#intents http://developer.android.com/guide/topics/data/data-storage.html#db

所有的帮助都是非常感谢的!谷歌的代码甚至被评论帮助,但我是新的,它仍然不清楚我。

对我来说,"getExternalStoragePublicDirectory“显然与它有关,但我不知道我是否应该用我的新道路或其他东西来取代这部分。

下面是代码片段:

onCreate:

代码语言:javascript
复制
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_video);

    //create new Intent
    Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);  // create a file to save the video
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);  // set the image file name

    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set the video image quality to high

    // start the Video Capture Intent
    startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
}

onActivityResult:

代码语言:javascript
复制
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // Video captured and saved to fileUri specified in the Intent
            Toast.makeText(this, "Video saved to:\n" +
                     data.getData(), Toast.LENGTH_LONG).show();
        } else if (resultCode == RESULT_CANCELED) {
            // User cancelled the video capture
        } else {
            // Video capture failed, advise user
        }
    }
}

getOutputMediaFile:

代码语言:javascript
复制
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_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "VID_"+ timeStamp + ".mp4");
    } else {
        return null;
    }

    return mediaFile;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-08 17:08:39

如果您想要创建自己的文件夹,则必须在这里进行更改。

代码语言:javascript
复制
 File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES), "MyCameraApp");

其中,"MyCameraApp“是您的文件夹名,因此您可以用您的文件夹替换,而Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)是您的目录,如果您只想保存sdcard,那么就用Environment.getExternalStorageDirectory替换。它在/sdcard/your_文件夹_name/your_video_file.mp4中创建一个文件夹。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23548114

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档