HB.
HB.

Reputation: 4226

Android - Saving video to internal storage

I currently use a FloatingActionButton to open the gallery and let the user pick a video, once the video has been selected it is opened in a videoView.

public class Activity extends AppCompatActivity {
    private static final int pick = 100;
    Uri videoUri;
    VideoView videoview;


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

        videoview = (VideoView) findViewById(R.id.videoview);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openGallery();

            }


        });
    }

    private void openGallery() {
        Intent intent = new Intent(Intent.ACTION_PICK,     MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, pick);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)     {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == pick) {
            videoUri = data.getData();
            videoview.setVideoURI(videoUri);
            videoview.start();

        }

    }

I want to save the video to the internal storage once the user selects it?

Upvotes: 0

Views: 12960

Answers (1)

Oyewo Remi
Oyewo Remi

Reputation: 426

Here is how you can save video to the internal storage after the user select it.

Inside onActivityResult code block, add the following:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)     {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK && requestCode == pick) {


        try {

            File newfile;

            AssetFileDescriptor videoAsset = getContentResolver().openAssetFileDescriptor(data.getData(), "r");
            FileInputStream in = videoAsset.createInputStream();

            File filepath = Environment.getExternalStorageDirectory();
            File dir = new File(filepath.getAbsolutePath() + "/" +"Your Folder Name" + "/");
            if (!dir.exists()) {
                dir.mkdirs();
            }

            newfile = new File(dir, "save_"+System.currentTimeMillis()+".mp4");

            if (newfile.exists()) newfile.delete();



            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("", "Copy file successful.");


            videoUri = data.getData();
            videoview.setVideoURI(videoUri);
            videoview.start();



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




    }

}

Note: Don't forget to add permission to the manifest.

Upvotes: 4

Related Questions