NuhaAbd
NuhaAbd

Reputation: 39

Import a video from the gallery then save it in another folder

The concept behind my application may not make sense, but I just need it to demonstrate something.
What I want to do is:

1 - import a video from android gallery
2 - rename it (xyz for example)
3 - save it under different folder (ABC)

The complete path will be, for example:

 "/storage/emulated/0/Pictures/ABC/XYZ.mp4" 

I managed to call the intent to display the gallery where I will pick the intended video, but I don't know what should I write in the onActivityResult?

 gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View v,
                            int position, long id) {

            Intent intent = new Intent();
                intent.setType("video/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent,"Select Video"),TAKE_Video);
        }
    }

I added this code following @CommonsWare steps but nothing happens :(

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {

  if (resultCode == RESULT_OK) {
        if (requestCode == TAKE_Video) {

            File root = new File(Environment.getExternalStorageDirectory(), "/Pictures/Targets/"); ///storage/emulated/0/Directory Name
            if (!root.exists()) {
                root.mkdirs();
            }
            try {

                FileInputStream fis = (FileInputStream) getContentResolver().openInputStream(data.getData());

                File file;
                String newname="newname.mp4";
                file = new File(root,newname);
                FileOutputStream fos = new FileOutputStream(file);
                byte[] buf = new byte[1024];
                int len;
                while ((len = fis.read(buf)) > 0) {
                    fos.write(buf, 0, len);

                }

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

Upvotes: 0

Views: 571

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007584

Step #1: In onActivityResult(), check the request code (1st parameter) to see if it is RESULT_LOAD_Video, and check the result code (2nd parameter) to see if it is RESULT_OK.

Step #2: When both checks from step #1 are true, create a FileOutputStream on your desired destination file, with the new name.

Step #3: Call getContentResolver().openInputStream(), passing in the value of getData() called on the Intent passed into onActivityResult() (3rd parameter).

Step #4: Use Java I/O to read bytes from the InputStream you got in step #3 and write them to the FileOutputStream that you got in step #2.

Step #5: When you are done copying the bytes, call flush(), getFD().sync(), and close(), respectively, on the FileOutputStream.

Step #6: When you have all of that working, move the code for steps #2-5 to a background thread, as you should not do this sort of I/O on the main application thread.

Upvotes: 1

Related Questions