其他分享
首页 > 其他分享> > android – 如何从相机Intent获取视频并将其保存到目录?

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

作者:互联网

是否可以使用类似于以下代码的视频相同的代码?

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

我想要做的是能够使用Camera Intent拍摄视频并将该视频或该视频的副本保存到我的特定目录.这是我必须剪辑的代码:

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

        }
    });
}

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

解决方法:

首先,您需要从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
 }

标签:android,filesystems,video,video-processing,android-camera-intent
来源: https://codeday.me/bug/20190614/1237420.html