skip to Main Content

I am trying to capture video via Gallery or via Camera. I am able to successfully fetch the video from Gallery. However when I try to record the video from camera it loses the track and I am unable to fetch the path. It does save the video on path. The log give following error/warning. Why is is losing track of the video recorder? Where am I going wrong?

2022-03-15 07:32:08.683 14318-14318/com.example.locationfetcher_v2 W/MirrorManager: this model don't Support
2022-03-15 07:32:13.864 14318-14318/com.example.locationfetcher_v2 D/DecorView: createDecorCaptionView windowingMode:1 mWindowMode 1 isFullscreen: true
2022-03-15 07:32:14.875 14318-14318/com.example.locationfetcher_v2 I/Timeline: Timeline: Activity_launch_request time:174742145
2022-03-15 07:32:14.912 14318-14395/com.example.locationfetcher_v2 D/OpenGLRenderer: endAllActiveAnimators on 0xb400007e4ade6e00 (AlertController$RecycleListView) with handle 0xb400007e52639220
2022-03-15 07:32:20.913 14318-14339/com.example.locationfetcher_v2 W/System: A resource failed to call close

Here is my code:

public void showPictureDialog(View view){
        AlertDialog.Builder pictureDialog = new AlertDialog.Builder(this);
        pictureDialog.setTitle("Select Action");
        String[] pictureDialogItems = {
                "Select video from gallery",
                "Record video from camera" };
        pictureDialog.setItems(pictureDialogItems,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                            case 0:
                                chooseVideoFromGallery();
                                break;
                            case 1:
                                takeVideoFromCamera();
                                break;
                        }
                    }
                });
        pictureDialog.show();
    }


    public void chooseVideoFromGallery() {
        Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);

        startActivityForResult(galleryIntent, PICK_VIDEO_GALLERY);
    }

    private void takeVideoFromCamera() {
        Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
        startActivity(intent);
    }

    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Video.Media.DATA };
        Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
        if (cursor != null) {
            // HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
            // THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
            int column_index = cursor
                    .getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } else
            return null;
    }

    private void saveVideoToInternalStorage (String filePath) {

        File newfile;

        try {

            File currentFile = new File(filePath);
//            show_notification("New File Video:" + Environment.getExternalStorageDirectory());
            File wallpaperDirectory = new File(Environment.getExternalStorageDirectory() + VIDEO_DIRECTORY);
            newfile = new File(wallpaperDirectory, Calendar.getInstance().getTimeInMillis() + ".mp4");

            if (!wallpaperDirectory.exists()) {
                wallpaperDirectory.mkdirs();
            }

            if(currentFile.exists()){

                InputStream in = new FileInputStream(currentFile);
                OutputStream out = new FileOutputStream(newfile);

                // Copy the bits from instream to outstream
                byte[] buf = new byte[1024];
                int len;

                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                in.close();
                out.close();
                show_notification("Video file saved successfully.");
            }else{
                show_notification("Video saving failed. Source file missing.");
            }
        } catch (Exception e) {
            show_notification(e.toString());
            e.printStackTrace();
        }

@RequiresApi(api = Build.VERSION_CODES.Q)
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        inputStreamImg = null;
        if (requestCode == PICK_VIDEO_GALLERY){
            if (data != null) {
                Uri contentURI = data.getData();

                String selectedVideoPath = getPath(contentURI);
                Log.d("path",selectedVideoPath);
//                show_notification("Saving...");
                saveVideoToInternalStorage(selectedVideoPath);
                vidView.setVideoURI(contentURI);
                vidView.requestFocus();
                vidView.start();
                videoPath = selectedVideoPath;
                show_notification("video Gallery - " + videoPath);
            } else if (requestCode == PICK_VIDEO_CAMERA) {
                Uri contentURI = data.getData();
                show_notification(contentURI.getPath());
                String recordedVideoPath = getPath(contentURI);
                show_notification(recordedVideoPath);
                saveVideoToInternalStorage(recordedVideoPath);

                videoPath = recordedVideoPath;

                vidView.setVideoURI(contentURI);
                vidView.requestFocus();
                vidView.start();
                show_notification("video Camera - " + videoPath);
            }
        }
    }

3

Answers


  1. I don’t know if it will help but maybe you should do startActivityForResult or ActivtyResultLauncher. I would comment that if I had enough reputation.

    Login or Signup to reply.
  2. Have you added record permission to you android manifest

    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    

    Also,

    Can you remove this@RequiresApi(api = Build.VERSION_CODES.Q) or handle case when target is not equal to Build.VERSION_CODES.Q

    @RequiresApi(api = Build.VERSION_CODES.Q)
    @Override
    public void onActivityResult{
    
    Login or Signup to reply.
  3. You are unable to get the video path due to the below errors:

    1.In your takeVideoFromCamera() method you start the intent using the startActivity(intent); instead of startActivityForResult(intent, PICK_VIDEO_CAMERA); so the onActivityResult() cannot be called to retrieve the video path. So you have to change your code like the below:

    private void takeVideoFromCamera() {
        Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
        startActivityForResult(intent, PICK_VIDEO_CAMERA);
    }
    

    2.After you make the above change the onActivityResult() gets called correctly but you are not handle correct the response because the statement of requestCode == PICK_VIDEO_CAMERA is inside the statement of if (requestCode == PICK_VIDEO_GALLERY){}. So you have to modify this like the below:

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        if (resultCode == Activity.RESULT_OK) 
        {
            if (requestCode == PICK_VIDEO_GALLERY) {
                if (data != null) {
                    Uri contentURI = data.getData();
                    vidView.setVideoURI(contentURI);
                    vidView.start();
                }
            }
            else if (requestCode == PICK_VIDEO_CAMERA) {
                if (data != null) {
                    Uri contentURI = data.getData();
                    vidView.setVideoURI(contentURI);
                    vidView.start();
                }
            }
        }
    }
    

    Because the startActivityForResult() now is deprecated i would suggest to change this using the new standard way which is the ActivityResultContracts.StartActivityForResult() by modifying your code to be like the below:

    private void takeVideoFromCamera() {
        Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
        if (getPackageManager().resolveActivity(intent, 0) != null) {
            videoActivityResultLauncher.launch(intent);
        } else {
            Toast.makeText(this, "No apps supports this action", Toast.LENGTH_SHORT).show();
        }
    }
    
    private final ActivityResultLauncher<Intent> videoActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
        @Override
        public void onActivityResult(ActivityResult result) {
            if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) {
                Uri contentURI = result.getData().getData();
                vidView.setVideoURI(contentURI);
                vidView.start();
            }
        }
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search