0

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();
            }
        }
NuhaAbd
  • 39
  • 9
  • 1
    "I managed to call the intent to display the gallery where I will pick the intended video" -- please edit your question and provide a [mcve], demonstrating what you are doing for this, as that will help steer recommendations for how to use the result. – CommonsWare Dec 03 '16 at 22:40
  • Sorry about that, I add the intent code – NuhaAbd Dec 03 '16 at 22:44

1 Answers1

1

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank you very much for your detailed answer. I'm struggling with the fourth step how is it done exactly? Sorry I'm a newbie at this – NuhaAbd Dec 04 '16 at 00:50
  • I added the code following the steps you mentioned but nothing happens..can you please check what am I doing wrong? – NuhaAbd Dec 04 '16 at 16:29
  • @NuhaAbd: You are calling `startActivityForResult()` and passing in `RESULT_LOAD_Video`. You are checking for `TAKE_Video` in `onActivityResult()`. These are not the same symbols and perhaps do not resolve to the same `int` values. – CommonsWare Dec 04 '16 at 16:30
  • Sorry my bad I didn't edite the previous posted code. I've changed it to Take video but nothing happens – NuhaAbd Dec 04 '16 at 16:39
  • @NuhaAbd: I am not sure how you are determining whether something happens or not. For example, if you are looking for the file via a desktop OS, [you have more work to do](http://stackoverflow.com/questions/32789157/how-to-write-files-to-external-public-storage-in-android-so-that-they-are-visibl). Beyond that, use a debugger or logging statements to see what is and is not getting executed. – CommonsWare Dec 04 '16 at 16:50
  • I'm connecting my device (Samsung Note 4 ) to it and run the application on it. The application runs and I can select the video but that it. What I'm expecting is to see the video selected in the new specified folder. so I check the gallery folder but nothing is there – NuhaAbd Dec 04 '16 at 16:58
  • 1
    @NuhaAbd: "so I check the gallery folder but nothing is there " -- I do not know what a "gallery folder" is. If you mean something inside of a gallery-style application, usually those apps use `MediaStore`, not the filesystem, and you have not done anything to update the `MediaStore` index. Your video will show up eventually (e.g., tomorrow). If you want it to show up faster, [you have more work to do](http://stackoverflow.com/questions/32789157/how-to-write-files-to-external-public-storage-in-android-so-that-they-are-visibl). – CommonsWare Dec 04 '16 at 17:01
  • You are my hero! Thank you ! I rebooted the mobile and taa daa all the folders and files are there!. – NuhaAbd Dec 04 '16 at 17:30