2

I take a photo and save it to external storage :

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File photoFile = getPhotoFile();

            String authorities = getActivity().getPackageName() + ".fileprovider";
            imageUri = FileProvider.getUriForFile(getActivity(), authorities, photoFile);

            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            startActivityForResult(intent, 1);

It works fine But what about recording a video
I wrote some code like this but i think it's not correct :

            Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
            File videoFile = getVideoFile();

            String authorities = getActivity().getPackageName() + ".fileprovider";
            videoUri = FileProvider.getUriForFile(getActivity(), authorities, videoFile);

            intent.putExtra(MediaStore.EXTRA_OUTPUT, videoUri);
            startActivityForResult(intent, 2);

What can i do?

EDIT: And these methods create a dir and return photo and video files

public File getPhotoFile() {
    //create a random name
    String randomName = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
    File mainPath = new File(path, "/Images");

    if (!mainPath.exists()) {

        mainPath.mkdirs();
    }

    File photoPath = new File(mainPath, randomName + ".jpg");
    return photoPath;
}

public File getVideoFile() {
    //create a random name
    String randomName = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
    File mainPath = new File(path, "/Videos");

    if (!mainPath.exists()) {

        mainPath.mkdirs();
    }

    File photoPath = new File(mainPath, randomName + ".mp4");
    return photoPath;
}
Khashayar
  • 326
  • 3
  • 15
  • You should take a look at this [question](https://stackoverflow.com/questions/1817742/how-can-i-record-a-video-in-my-android-app) – Oleg Sokolov Aug 07 '18 at 11:51
  • have a look at following link - https://stackoverflow.com/questions/10278865/record-save-and-play-a-video-in-android – Rajshree Tiwari Aug 07 '18 at 11:52
  • 1
    "but i think it's not correct" -- that should be OK, assuming that your `FileProvider` is working. There may be bugs or limitations in the camera app on the device that prevent it from using `EXTRA_OUTPUT`. And I would add `FLAG_GRANT_READ_URI_PERMISSIONS` and `FLAG_GRANT_WRITE_URI_PERMISSIONS` to the `Intent`, just to be safe. See [this sample app](https://github.com/commonsguy/cw-omnibus/tree/v8.12/Media/VideoRecordIntent). – CommonsWare Aug 07 '18 at 11:53
  • @CommonsWare Yeah you right i used Genymotion...now i test app in andy and works fine but still doesn't work in Genymotion ..i tried all api – Khashayar Aug 07 '18 at 18:04
  • 1
    "but still doesn't work in Genymotion" -- we cannot help you without a detailed explanation of what "doesn't work" means. Personally, I never bother testing camera-related code on an emulator. I only test such code on actual Android hardware. – CommonsWare Aug 07 '18 at 22:13

2 Answers2

0

To record video use:

startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);

Here is an example

Oleg Sokolov
  • 1,134
  • 1
  • 12
  • 19
0

in your recordVideo.onClick() method add this code

 if (checkPermissions(YourActivity.this)) {
                captureVideo();
            } else {
                requestCameraPermission(MEDIA_TYPE_VIDEO);
            } 


public static boolean checkPermissions(Context context) {
    return ActivityCompat.checkSelfPermission(context, Manifest.permission.CAMERA) == 
   PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(context, 
   Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(context, 
 Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED;
}

//in CaptureVideo method

private void captureVideo() {
    Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

    File file = getOutputMediaFile(MEDIA_TYPE_VIDEO);
    if (file != null) {
        imageStoragePath = file.getAbsolutePath();
    }

    Uri fileUri = getOutputMediaFileUri(getApplicationContext(), file);

    // set video quality
    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file

    // start the video capture Intent
    startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE);
}

/** * Creates and returns the image or video file before opening the camera */

 public static File getOutputMediaFile(int type) {

    // External sdcard location
    File mediaStorageDir = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            YourActivity.GALLERY_DIRECTORY_NAME);

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.e(YourActivity.GALLERY_DIRECTORY_NAME, "Oops! Failed create "
                    + YourActivity.GALLERY_DIRECTORY_NAME + " directory");
            return null;
        }
    }


 public static Uri getOutputMediaFileUri(Context context, File file) {
    return FileProvider.getUriForFile(YourActivity.this, context.getPackageName() + 
   ".provider", file);
}


 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (requestCode == CAMERA_CAPTURE_VIDEO_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // Refreshing the gallery
            refreshGallery(getApplicationContext(), imageStoragePath);

            // video successfully recorded
           showVideo();
        } else if (resultCode == RESULT_CANCELED) {
            // user cancelled recording
            Toast.makeText(getApplicationContext(),
                    "cancelled video recording", Toast.LENGTH_SHORT)
                    .show();
        } else {
            // failed to record video
            Toast.makeText(getApplicationContext(),
                    "Sorry! Failed to record video", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}


public static void refreshGallery(Context context, String filePath) {
    // ScanFile so it will be appeared on Gallery
    MediaScannerConnection.scanFile(context,
            new String[]{filePath}, null,
            new MediaScannerConnection.OnScanCompletedListener() {
                public void onScanCompleted(String path, Uri uri) {
                }
            });
}


  private void showVideo() {
    try {

        videoView.setVideoPath(imageStoragePath);
        // video playing
        videoView.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
 }