Copy selected audio file to a folder on android

0

I am learning how to program app for android, I am a bit stuck and I can not find information about it.

My intention is for the user to select an audio file through:

Intent.ACTION_GET_CONTENT, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI)

And then copy that file to a folder (either in the internal project folder or in the app's external memory folder.)

But I do not get it. I tried to do it with inputstream and outputstream (I did the test of copying an audio file of the folder of the app in the external memory to the internal folder of the app with this code and it worked), but when I created the File origin obtaining the uri that returns the intent, gives error:

W/System.err: java.io.FileNotFoundException: /document/392: open failed: ENOENT (No such file or directory).

And I tried passing it origen.getAbsoluteFile() :

W/System.err: java.io.FileNotFoundException: /content:/com.android.providers.downloads.documents/document/392: open failed: ENOENT (No such file or directory)

Code:

            Uri sonidoPath = data.getData();
            String sonidoRuta= sonidoPath.getPath();

            File origen = new File(sonidoRuta);
            File destino = new File(audioUri);

            try {
                InputStream in = new FileInputStream(origen);

                OutputStream out = new FileOutputStream(destino);

                byte[] buf = new byte[1024];
                int len;

                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }

                in.close();
                out.close();
            } catch (IOException ioe){
                ioe.printStackTrace();
            }

I do not know why it gives an error that the file does not exist, and even less knowing that the code works, because it copies and pastes an audio file if I create the source file by passing it a string with the source file path.

I hope I have explained myself well, and that someone can help me.

Thanks, a greeting.

    
asked by PABLO MILLAN 08.06.2018 в 19:34
source

0 answers