Skip to content Skip to sidebar Skip to footer

Accessing Data From Custom Content Providers

I've two content provider based apps A and B. Both have their own content providers and are setup for reading data from A and B and vice versa. Everything works fine when the other

Solution 1:

You can not initialize the content provider from somewhere else in your code like this, as the ContentProvider might be the first (or only) component of your app that's instantiated.

However, you can read the authority dynamically from the Manifest or a String resource. In my answer on Does Android Content Provider authority definition break the DRY rule? I outlined how we that in our OpenTasks-Provider.

Solution 2:

I don't see a call to init() in content provider. Is it only called from elsewhere as part of the application's start ?

If so that may explains why the content provider fails when the application is not already started : In this case the UriMatcher is empty and the switch in the query() method falls back to default witch throws the IllegalArgumentException.

You should either call init() in onCreate() or fully initialize the UriMatcher in the static initializer.

Solution 3:

You are setting the authority as

privatevoidsetAuthority(String packageName, boolean isPackageName) {
        if (isPackageName) {
            mAuthority = packageName + ".myprovider";
        } else {
            mAuthority = packageName;
        }
    }

So your mAuthority is either com.example.provider or com.example.provider.myprovider

However, you have defined authorities in the manifest as

android:authorities="com.example.${applicationId}-provider"

that is com.example.appA-provider

Post a Comment for "Accessing Data From Custom Content Providers"