We're using Mobile Iron to wrap our app.
I'm facing problems while inserting a ringtone in the MediaStore
.
I've read many questions but none are up-to-date: Indeed, MediaStorage.Audio.Media.DATA
is deprecated since Android 10 (Q). Moreover, it seems that, searching for a solution, in "Post" Android Q, the content Uri has to be accessed another way.
So I have to separate code for "Pre" and "Post" Android Q.
Here is my code:
val collection = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
} else {
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
}
val values = ContentValues()
values.put(MediaStore.Audio.Media.TITLE, ringtoneName)
values.put(MediaStore.Audio.Media.DISPLAY_NAME, "ringtone.wav"))
values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/x-wav")
values.put(MediaStore.Audio.Media.IS_RINGTONE, true)
values.put(MediaStore.Audio.Media.IS_MUSIC, false)
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false)
values.put(MediaStore.Audio.Media.IS_ALARM, false)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
Log.e("Ringtone", "Android 9 : Extracting file in external Storage")
extractRingtone(context)
var file: File? = null
try {
file = File(context.getExternalFilesDir(Environment.DIRECTORY_RINGTONES)!!.canonicalFile, "ringtone.wav")
} catch (e: IOException) {
e.printStackTrace()
}
values.put(MediaStore.Audio.Media.SIZE, file?.length())
values.put(MediaStore.Audio.Media.DATA, file?.absolutePath)
}
else
{
val inputStream = context.resources.openRawResource(R.raw.ringtone)
val size = inputStream.available()
values.put(MediaStore.Audio.Media.SIZE, size)
inputStream.close()
values.put(MediaStore.Audio.Media.IS_PENDING, true)
}
var newUri: Uri? = null
try {
newUri = resolver.insert(collection, values)
}
catch (exception: Exception)
{
Log.e("Ringtone", "Exception while inserting data", exception)
}
Then, in "Post" Android Q, I try to insert the ringtone in the content provider using resolver.openOutputStream(newUri)
, and then, update the ringtone with MediaStore.Audio.Media.IS_PENDING
set to ````false``` but newUri is always null and Exception is never thrown.
The code works correctly when the app isn't wrapped.
Any idea?
Edit: Added MediaStore.Audio.Media.IS_PENDING
value in ContentValues, but it didn't do the trick.
Edit 2: Updated question as we now the problem isn't the code but the wrapping of the app using Mobile Iron.