i am trying to save the audio files from the cache to a music directory having a folder Voice Changer
the below code is working fine in android 10 and above but below that its not working I'm currently facing a challenge while working with the MediaStore API in Android 8. This API is used for managing media files like images and videos in Android applications. Specifically, I'm having trouble adding files using this API. In Android development, being able to effectively handle media files is crucial, but I've encountered obstacles in this process. I've tried to dig into the problem, but the solution has eluded me so far. I'm exploring different avenues and troubleshooting methods to pinpoint the problem and find a resolution. I believe that with a bit more effort and perhaps some external insights, I can overcome this hurdle and continue with my Android development projects effectively
suspend fun String.saveAudioToGallery(
context: Context, mimeType: String = "audio/x-wav", done: (String?) -> Unit, error: () -> Unit
) = withContext(Dispatchers.IO) {
var savedPath: String? = null
val contentValues = ContentValues().apply {
put(MediaStore.Audio.Media.DISPLAY_NAME, "Vc_${System.currentTimeMillis()}.wav")
put(MediaStore.Audio.Media.MIME_TYPE, mimeType)
put(MediaStore.Audio.Media.DATE_ADDED, System.currentTimeMillis() / 1000)
}
val collection = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
contentValues.put(
MediaStore.Audio.Media.RELATIVE_PATH,
"${Environment.DIRECTORY_MUSIC}/VoiceChanger"
)
MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
} else {
val directory =
File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).absolutePath,"VoiceChanger")
if (!directory.exists()){
directory.mkdirs()
}
contentValues.put(MediaStore.Audio.Media.DATA, directory.absolutePath)
MediaStore.Audio.Media.getContentUri(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI.toString())
}
val contentResolver = context.contentResolver
try {
// Insert the audio file into the MediaStore and get the URI
val audioUri = contentResolver.insert(collection, contentValues)
Log.d("inputStram", "saveAudioToGalleryUri:$audioUri ")
audioUri?.let { uri ->
// Open an output stream using the ContentResolver
contentResolver.openOutputStream(uri)?.use { os ->
// Open input stream from the file path
val file = File(this@saveAudioToGallery)
val inputStream = FileInputStream(file)
// Write audio data to the output stream
inputStream.copyTo(os)
Log.d("inputStram", "saveAudioToGallery:$savedPath ")
savedPath = file.absolutePath // Save the file path for later use
Log.d("inputStram", "saveAudioToGallery:$savedPath ")
os.close()
inputStream.close()
}
}
} catch (e: Exception) {
// Handle exceptions
Log.e("TAG", "saveAudioToGallery: ${e.message}")
}
withContext(Dispatchers.Main) {
if (savedPath != null) {
Log.d("TAGGing", "saveAudioToGallery:$savedPath ")
done.invoke(savedPath)
} else {
Toast.makeText(context.applicationContext, "Failed to save audio to gallery", Toast.LENGTH_SHORT).show()
error.invoke()
}
}
}
For below 10 you use an impossible collection which causes for an exception that you did not tell us.
Further you should give the .DATA columf full path of the file. Not only full path of the directory.