How to get video from an Intent camera and save it in a directory?

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

I am trying to make a video using Camera Intent and save this video or a copy of this video in my specific directory. This is the code that the clip should take:

 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 do this to get this particular video that was just taken and then copied it to the sd / appname / project_name / directory.

The same goes for getting a name / file when adding a clip 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.

+4
source share
1 answer

First you need to get the URI from onActivityResult as follows:

 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 saved as videoPath, you can save this with

 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 } 
+7
source

All Articles