如何从相机Intent中获取视频并将其保存到目录中?

发布于 2024-11-02 17:32:46 字数 1865 浏览 4 评论 0原文

是否可以使用类似于以下内容的代码对视频执行相同的操作?

        if (resultCode == Activity.RESULT_CANCELED) {
            // camera mode was canceled.
        } else if (resultCode == Activity.RESULT_OK) {

            // Took a picture, use the downsized camera image provided by default
            Bitmap cameraPic = (Bitmap) data.getExtras().get("data");
            if (cameraPic != null) {
                try {
                    savePic(cameraPic);
                } catch (Exception e) {
                    Log.e(DEBUG_TAG, "saveAvatar() with camera image failed.", e);
                }
            }

我想要做的是能够使用相机意图拍摄视频并将该视频或该视频的副本保存到我的特定目录中。这是我必须拍摄剪辑的代码:

private void initTakeClip(){
    Button takeClipButton = (Button) findViewById(R.id.takeClip);
    takeClipButton.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            String strVideoPrompt = "Take your Video to add to your timeline!";
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
            startActivityForResult(Intent.createChooser(cameraIntent, strVideoPrompt), TAKE_CLIP_REQUEST);
            }
    });
}

我只是不知道如何获取刚刚拍摄的特定视频,然后将其复制到我的 sd/appname/project_name/ 目录中。

将已从内存中的剪辑添加到我的目录时获取名称/文件位置的情况也是如此:

 private void initAddClip(){
    Button addClipButton = (Button) findViewById(R.id.addClip);
    addClipButton.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            String strAvatarPrompt = "Choose a picture to use as your avatar!";
            Intent pickVideo = new Intent(Intent.ACTION_PICK);
            pickVideo.setType("video/*");
            startActivityForResult(Intent.createChooser(pickVideo, strAvatarPrompt), ADD_CLIP_REQUEST);

        }
    });
}

将不胜感激任何/所有帮助。

Is it possible to have code similar to the following that does the same for video?

        if (resultCode == Activity.RESULT_CANCELED) {
            // camera mode was canceled.
        } else if (resultCode == Activity.RESULT_OK) {

            // Took a picture, use the downsized camera image provided by default
            Bitmap cameraPic = (Bitmap) data.getExtras().get("data");
            if (cameraPic != null) {
                try {
                    savePic(cameraPic);
                } catch (Exception e) {
                    Log.e(DEBUG_TAG, "saveAvatar() with camera image failed.", e);
                }
            }

What I am trying to do is to be able to take a video using the Camera Intent and save that video or a copy of that video to my specific directory. This is the code i have to take the clip:

private void initTakeClip(){
    Button takeClipButton = (Button) findViewById(R.id.takeClip);
    takeClipButton.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            String strVideoPrompt = "Take your Video to add to your timeline!";
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
            startActivityForResult(Intent.createChooser(cameraIntent, strVideoPrompt), TAKE_CLIP_REQUEST);
            }
    });
}

I just don't know how to go about then getting that specific video that was just taken and then copying it into my sd/appname/project_name/ directory.

The same is the case for getting the name/file location when adding a clip already from memory to my directory:

 private void initAddClip(){
    Button addClipButton = (Button) findViewById(R.id.addClip);
    addClipButton.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            String strAvatarPrompt = "Choose a picture to use as your avatar!";
            Intent pickVideo = new Intent(Intent.ACTION_PICK);
            pickVideo.setType("video/*");
            startActivityForResult(Intent.createChooser(pickVideo, strAvatarPrompt), ADD_CLIP_REQUEST);

        }
    });
}

Any/All help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

郁金香雨 2024-11-09 17:32:46

首先,您需要做的是从 onActivityResult 获取 URI,如下所示:

private String videoPath = "";

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    Uri vid = data.getData();
    videoPath = getRealPathFromURI(vid);


}

public String getRealPathFromURI(Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(contentUri, proj, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

然后,一旦您将实际路径存储为 videoPath,则可以使用以下方法存储该路径:

try {

  FileInputStream fis = openFilePath(videoPath);

  //this is where you set whatever path you want to save it as:

  File tmpFile = new File(Environment.getExternalStorageDirectory(),"VideoFile.3gp"); 

  //save the video to the File path
  FileOutputStream fos = new FileOutputStream(tmpFile);

  byte[] buf = new byte[1024];
  int len;
  while ((len = fis.read(buf)) > 0) {
    fos.write(buf, 0, len);
  }       
  fis.close();
  fos.close();
 } catch (IOException io_e) {
    // TODO: handle error
 }

First what you need to do is get the URI from the onActivityResult like this:

private String videoPath = "";

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    Uri vid = data.getData();
    videoPath = getRealPathFromURI(vid);


}

public String getRealPathFromURI(Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(contentUri, proj, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

Then once you have the actual path stored as videoPath then you can store that using

try {

  FileInputStream fis = openFilePath(videoPath);

  //this is where you set whatever path you want to save it as:

  File tmpFile = new File(Environment.getExternalStorageDirectory(),"VideoFile.3gp"); 

  //save the video to the File path
  FileOutputStream fos = new FileOutputStream(tmpFile);

  byte[] buf = new byte[1024];
  int len;
  while ((len = fis.read(buf)) > 0) {
    fos.write(buf, 0, len);
  }       
  fis.close();
  fos.close();
 } catch (IOException io_e) {
    // TODO: handle error
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文