Uri null after null check - SMS Content provider [Android SDK 30+]

256 Views Asked by At
    public void save(AppCompatActivity context, SMSListener listener) {
    mContext = context;
    try {
    //  String mode = SMSData.getSMS_typeText(type_internal);
        ContentValues values = returnContentValues(); // content values
        ContentResolver resolver = context.getContentResolver();
        Uri uri = Telephony.Sms.CONTENT_URI;
        if (resolver != null && uri != null)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                ContentProviderClient client =
                        resolver.acquireContentProviderClient(Telephony.Sms.CONTENT_URI);
                client.insert(uri, values);
                client.release();
            } else {
                resolver.insert(uri, values);
            }
        else {
            listener.onCompleted(false);
            return;
        }
        listener.onCompleted(true);
    } catch (Exception ex) {
        ex.printStackTrace();
        listener.onCompleted(false);
    }

So, this is the code I've wrote till now trying to write SMS content provider which is working well when targeting SDK Level of <30, but for >= 30, it's throwing the below exception. (tried to insert directly with Contentresolver too instead of ContentProviderClient, but the exception was same! on 30+)

System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getAuthority()' on a null object reference
System.err:     at android.os.Parcel.createExceptionOrNull(Parcel.java:2379)
System.err:     at android.os.Parcel.createException(Parcel.java:2357)
System.err:     at android.os.Parcel.readException(Parcel.java:2340)
System.err:     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:190)
System.err:     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:142)
System.err:     at android.content.ContentProviderProxy.insert(ContentProviderNative.java:549)
System.err:     at android.content.ContentProviderClient.insert(ContentProviderClient.java:348)
System.err:     at android.content.ContentProviderClient.insert(ContentProviderClient.java:337)
System.err:     at xxxxx.zzzz.dddd.save(SMSCreator.java:94)

I've already checked the authority for SMS content resolver isn't null (by writing a test code), then I came to know about the package-visibility related issues in Android Q, so I included this in manifest & included QUERY_ALL_PACKAGES too:

<queries>
    <package android:name="string" />
    <provider
        android:authorities="list"
        android:exported="false" />
</queries>

Just to be sure I also run some bunch of code that prints the authorities of SMS providers and the output included :

[content://sms, content://sms-changes, content://mms-sms]

I've also made the app default SMS app with all the required permissions. What am I missing so the code isn't working in Android Q+?

[edit] Also I checked the neither Uri nor it's authority value is null then why getAuthority() is throwing null? I've used the similar code to Google's messaging app, still the same result.

UPDATE : Though I've done null check on the uri, it says on a Android 10 device

W/System.err: java.lang.NullPointerException: Uri must not be null
W/System.err:     at android.os.Parcel.createException(Parcel.java:2077)
W/System.err:     at android.os.Parcel.readException(Parcel.java:2039)
W/System.err:     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:188)
W/System.err:     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
W/System.err:     at android.content.ContentProviderProxy.insert(ContentProviderNative.java:481)
W/System.err:     at android.content.ContentProviderClient.insert(ContentProviderClient.java:318)
W/System.err:     at xxxxx.zzzz.dddd.save(SMSCreator.java:94)

(using Pixel 5/Moto One)

1

There are 1 best solutions below

1
Spectator On

Try to use this permission


mActivity.getActivityResultRegistry().register("key", new ActivityResultContracts.OpenDocument(), result -> {
                        mActivity.getApplicationContext().getContentResolver().takePersistableUriPermission(
                                result,
                                Intent.FLAG_GRANT_READ_URI_PERMISSION
                        );