0

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.

Jeje Doudou
  • 1,707
  • 1
  • 16
  • 24
  • 1
    Use column IS_PENDING twice. – blackapps Jun 15 '21 at 15:22
  • Just read some stackoverflow pages tagged `mediastore`. – blackapps Jun 15 '21 at 20:23
  • Ok, found it. I'll try tomorrow. Thanks for your help. – Jeje Doudou Jun 15 '21 at 20:29
  • Have you considered SoundPool for playing sounds? https://developer.android.com/reference/android/media/SoundPool I found it to be very easy, and I have a class that implements sounds easily. If you're interested I can post the code. – ezaspi Jun 15 '21 at 21:59
  • @blackapps Thanks for your help mate, but unfortunately, this didn't do the trick. :( I will edit my code with the added lines. – Jeje Doudou Jun 16 '21 at 07:45
  • @ezaspi The aim here is to set the file as a ringtone for specific contacts. I don't wantt to play it. – Jeje Doudou Jun 16 '21 at 07:46
  • 1
    `facing problems while inserting a ringtone in the ContentResolver` No. You try to insert a file in the media store. Try without these other IS_... values. – blackapps Jun 16 '21 at 08:01
  • @blackapps yes, you're right. I edit my question. – Jeje Doudou Jun 16 '21 at 08:02
  • Tried your code (as Java) and it gives nice uries from .insert() on Android 11. – blackapps Jun 16 '21 at 08:49
  • @blackapps I forgot to mention that the app is "wrapped" with Mobile Iron. And, you're right, when I don't wrap it, the code works correctly. So the problem comes from the wrapping and not from my code. Thanks for your help, I'll close the subject. – Jeje Doudou Jun 16 '21 at 14:53

1 Answers1

0

As pointed by @blackapps , the code works correctly, indeed. The thing I didn't mention is that the app is "wrapped" using Mobile Iron. When not wrapped, the code works normally and I don't get the error.

Thanks to him, I came to the conclusion that the wrapping of the app causes my problem, as it makes the app sandboxed and apparently denies the write of any file in the phone.

Anyway, this is not a problem with Android nor Kotlin, but with Mobile Iron.

So I updated my question, its tags and its title.

Jeje Doudou
  • 1,707
  • 1
  • 16
  • 24