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

6

是否有类似以下代码的视频版本?

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

        }
    });
}

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

1个回答

8

首先你需要做的是从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
 }

3
FileInputStream fis = openFilePath(videoPath);在这个语句中,我感到困惑,我无法理解它...... 你能帮帮我吗? - Kalpesh
1
是的,openFilePath 是什么......new FileInputStream(realURIhere) 是什么? - Michael Paccione

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接