Trying to set an audio asset as a ringtone on Android

143

It says that there is an error in URI and you need to fix it (URI is null).

Possible solutions:

  1. Check the URI and the sound file name.

  2. If it's working on emulator but not on the phone, check the OS version of both of them.

  3. You might get the permissions in just one AndroidManifest.xml file. Apply it on both AndroidManifest.xml files in debug and release folders.

Share:
143
Uzair Ashraf
Author by

Uzair Ashraf

I discovered my passion for software at a very early age. Most of my childhood was spent glued to a computer. I studied web development at Fullerton College for three years and received a certificate in web development. Fast forward a few years, I landed at LearningFuze and was given the opportunity to spread my love for all things software to new developers. You will find me in my freetime working on side projects, playing with various frameworks, and expanding my knowledge on the regular.

Updated on November 30, 2022

Comments

  • Uzair Ashraf
    Uzair Ashraf 11 months

    I'm using the following package to set a an audio asset as a ringtone ringtone_set

    This is the error I keep encountering when calling the setRingtone method.

    W/System.err(15204): java.lang.NullPointerException: uri
    W/System.err(15204):    at com.android.internal.util.Preconditions.checkNotNull(Preconditions.java:128)
    W/System.err(15204):    at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1229)
    W/System.err(15204):    at android.content.ContentResolver.openOutputStream(ContentResolver.java:1009)
    W/System.err(15204):    at android.content.ContentResolver.openOutputStream(ContentResolver.java:985)
    W/System.err(15204):    at acr.rt.ringtone_set.RingtoneSetPlugin.setThings(RingtoneSetPlugin.java:109)
    W/System.err(15204):    at acr.rt.ringtone_set.RingtoneSetPlugin.onMethodCall(RingtoneSetPlugin.java:151)
    W/System.err(15204):    at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233)
    W/System.err(15204):    at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
    W/System.err(15204):    at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:692)
    W/System.err(15204):    at android.os.MessageQueue.nativePollOnce(Native Method)
    W/System.err(15204):    at android.os.MessageQueue.next(MessageQueue.java:325)
    W/System.err(15204):    at android.os.Looper.loop(Looper.java:151)
    W/System.err(15204):    at android.app.ActivityThread.main(ActivityThread.java:6724)
    W/System.err(15204):    at java.lang.reflect.Method.invoke(Native Method)
    W/System.err(15204):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    W/System.err(15204):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
    

    I am using another package to request permissions to access the users storage. If the library can't find the asset then it will log a message to the console. I can play the sound just fine as well, its just when I try to set the ringtone.

    Here is the AndroidManifest.xml

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        package="com.example.anisoundboard2">
        <uses-permission android:name="android.permission.WRITE_SETTINGS" tools:ignore="ProtectedPermissions"/>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
        <uses-permission android:name="android.permission.RECORD_AUDIO" />
    

    Here is how I am loading the assets in pubspec.yaml

    flutter:
    
      # The following line ensures that the Material Icons font is
      # included with your application, so that you can use the icons in
      # the material Icons class.
      uses-material-design: true
    
      # To add assets to your application, add an assets section, like this:
      assets:
        - assets/images/
        - assets/sounds/
    

    Here is the function that is called when the user clicks Set as Ringtone

      Future setRingtone(String soundFileName) async {
        try {
          await requestStoragePermission();
          await RingtoneSet.setRingtone('assets/sounds/$soundFileName');
        } catch(e) {
          print(e);
        }
      }
    

    And this is the function I use to request permission

      Future requestStoragePermission() async {
        final status = await Permission.storage.request();
        print(status);
      }
    

    This worked on my Android Emulator, but I am currently testing on an actual device. The LG Phoenix 4

  • basudev nayak
    basudev nayak almost 3 years
    you should have commented this instead of posting an answer.
  • Uzair Ashraf
    Uzair Ashraf almost 3 years
    I actually did use this one, the bug was not fixed
  • Uzair Ashraf
    Uzair Ashraf almost 3 years
    After going off these recommendations, I discovered that works on Android 10.0 and up. So now I just don't render the set ringtone button if the version is too low. Thank you for your insight