Copying or moving a file inside Internal storage on Android

373 Views Asked by At

In an Android app the user can record his voice. The sound recording is saved in a file defined by:

audioTempFile = File(getFilesDir(), "Audio_Temp_File")

Then I want to give the user the choice of the final folder and file name for the file to be saved. So the audioTempFile above can be used for a possible next recording without destroying the current one.

For example let us assume I want the file to be saved in this file called finalFile :

val rootDir = getFilesDir()
val storeDir = File(rootDir, "MyStorageDirectory")
val finalFile = File(storeDir, "MyFinalFileName")

How can I move or possibly copy audioTempFile to finalFile?

I did not find any clear answer by searching the net.

1

There are 1 best solutions below

13
M Ashhal On

I don't know about saving to a specific location, but this is the way I'm saving the recordings in my app.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){
                        try{
                            val values = ContentValues()
                            values.put(MediaStore.MediaColumns.DISPLAY_NAME, allFiles[position].name)
                            values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3")
                            values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_MUSIC)
                            val savedAudio = context.contentResolver.insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, values)
                            val outputStream = savedAudio?.let {
                                context.contentResolver.openOutputStream(it)
                            }
                            val fis = FileInputStream(allFiles[position])
                            var length : Int
                            val buffer = ByteArray(8192)
                            while (fis.read(buffer).also { length = it } > 0) {
                                outputStream?.write(buffer, 0,length)
                            }
                            Toast.makeText(context, "Audio Saved to Music Folder", Toast.LENGTH_SHORT).show()
                        }catch (e : IOException){
                        Toast.makeText(
                            context,
                            "There was an error saving the file",
                            Toast.LENGTH_SHORT
                        ).show()
                        e.printStackTrace()
                    }
                    }else{
                        try {
                            val audioDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)
                            val audio = File(audioDir, allFiles[position].name)
                            val fis = FileInputStream(allFiles[position])
                            val fos = FileOutputStream(audio)
                            var length : Int
                            val buffer = ByteArray(8192)
                            while (fis.read(buffer).also { length = it } > 0){
                                fos.write(buffer,0,length)
                            }
                            Toast.makeText(context, "Audio Saved to Music Folder", Toast.LENGTH_SHORT).show()
                        }catch (e : IOException){
                            Toast.makeText(
                                context,
                                "There was an error saving the file",
                                Toast.LENGTH_SHORT
                            ).show()
                            e.printStackTrace()
                        }
                    }

You would need MANAGE_EXTERNAL_STORAGE permission if u want to save files other than public directories. It's recommended to use MediaStore API to save your files.

Do correct me if I'm wrong as I'm still learning.

Hope this helps u :)