Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save video to Internal Storage

I have a videoView that plays an video from a path that corresponds to a video file within the android gallery

 VideoView videoView1 = (VideoView)promptsView.findViewById(R.id.videoView09);
            String SrcPath = "/storage/emulated/0/DCIM/Camera/20150824_210148.mp4";
            videoView1.setVideoPath(SrcPath);
            videoView1.requestFocus();
            videoView1.start();

Now, i need to somehow store the video from this videoView to the internal storage for my app privately.

I've managed to do this for photos using

public String saveImageToInternalStorage(Bitmap image, String imgRequestedName) {
    ContextWrapper cw = new ContextWrapper(getActivity());
    File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
    File mypath=new File(directory,imgRequestedName+".jpg");
    String loc = mypath.getAbsolutePath();
    FileOutputStream fos = null;
    try {

        fos = new FileOutputStream(mypath);
        image.compress(Bitmap.CompressFormat.JPEG, 70, fos);
        SharedPreferences pref = getActivity().getApplicationContext().getSharedPreferences("MyPref", Context.MODE_PRIVATE);
        final SharedPreferences.Editor editor = pref.edit();
        editor.putInt("totalImageCount",(pref.getInt("totalImageCount",0))+1);
        editor.commit();

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

    return mypath.getAbsolutePath();
}

How can i do the equivalent for videos?

and how can i read the video from the internal storage?

like image 427
Zachary Wathen Avatar asked Oct 20 '25 11:10

Zachary Wathen


1 Answers

Here is the codes to save video to internal storage for your app privately. And also code to read the video from the internal storage. Hope this helps.

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

            in.close();
            out.close();

            Log.v("", "Video file saved successfully.");

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



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

}

private void loadVideoFromInternalStorage(String filePath){

    Uri uri = Uri.parse(Environment.getExternalStorageDirectory()+filePath);
    myVideoView.setVideoURI(uri);

}
like image 193
Oyewo Remi Avatar answered Oct 23 '25 00:10

Oyewo Remi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!