1

I am developing app using camera on fragment,so for camera I have used this library https://natario1.github.io/CameraView/home because I am using watermarks in camera, now I want to save pictures and videos to gallery, for pictures I have done with below code, for video I done research but not get a solution, for pictures this library is giving bitmap so pictures are saving perfectly but for video I can't figure out kindly help me.

Thank you

public class VideoPreviewActivity extends AppCompatActivity {

    private VideoView videoView;

    private static VideoResult videoResult;

    private Button btnSaveVideo;


    public static void setVideoResult(@Nullable VideoResult result) {
        videoResult = result;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_preview);

        final VideoResult result = videoResult;
        if (result == null) {
            finish();
            return;
        }

        btnSaveVideo = findViewById(R.id.save_video);

        videoView = findViewById(R.id.video);
        videoView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                playVideo();
            }
        });

        AspectRatio ratio = AspectRatio.of(result.getSize());
        MediaController controller = new MediaController(this);
        controller.setAnchorView(videoView);
        controller.setMediaPlayer(videoView);
        videoView.setMediaController(controller);
        videoView.setVideoURI(Uri.fromFile(result.getFile()));
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                ViewGroup.LayoutParams lp = videoView.getLayoutParams();
                float videoWidth = mp.getVideoWidth();
                float videoHeight = mp.getVideoHeight();
                float viewWidth = videoView.getWidth();
                lp.height = (int) (viewWidth * (videoHeight / videoWidth));
                videoView.setLayoutParams(lp);
                playVideo();

                if (result.isSnapshot()) {
                    // Log the real size for debugging reason.
                    Log.e("VideoPreview", "The video full size is " + videoWidth + "x" + videoHeight);
                }
            }
        });

        //Click this button to save video
        btnSaveVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //Calling 1st Method but not working
                //SaveVideo(videoResult.getFile().toString());

                //2nd method calling but still not working
                saveVideoToInternalStorage(videoResult.getFile().toString());
            }
        });


    }

    void playVideo() {
        if (!videoView.isPlaying()) {
            videoView.start();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (!isChangingConfigurations()) {
            setVideoResult(null);
        }
    }



    //first method which I tried but not working

    private void SaveVideo(Uri f) {


        //I don't know what data type I have to pass in the methods parameters

        String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getAbsolutePath();
        File myDir = new File(root + "/Push Videos");
        if (!myDir.exists()) {
            myDir.mkdirs();
        }
        Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        String fname = "Video-"+ n +".mp4";
        File file = new File (myDir, fname);
        if (file.exists ())
            file.delete ();
        try {
            FileOutputStream out = new FileOutputStream(file);

            Toast.makeText(VideoPreviewActivity.this, "PUSH Video Saved", Toast.LENGTH_LONG).show();
            out.flush();
            out.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //Tried this 2nd method but it is also not giving a desired result

    //For saving Video...

    private void saveVideoToInternalStorage (String filePath) {

        File newfile;

        try {

            File currentFile = new File(filePath);
            String fileName = currentFile.getName();

            ContextWrapper cw = new ContextWrapper(getApplicationContext());
            File directory = cw.getDir("videoDir", Context.MODE_PRIVATE);


            newfile = new File(directory.getAbsolutePath(), fileName);

            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);
                }
                scanner(filePath);
                in.close();
                out.close();

                Log.v("", "Video file saved successfully.");
                Toast.makeText(this, "Push Video Saved TO Gallery", Toast.LENGTH_SHORT).show();

            }else{
                Log.v("", "Video saving failed. Source file missing.");
            }

        } catch (Exception e) {
            Log.d("Err ", "EXS " + e.getMessage());
            e.printStackTrace();
        }

    }
}
M Zaid Hayat
  • 11
  • 10

0 Answers0