I'm relatively new to ContentProviders in Android and I liked the idea of sharing asset files between apps. Let's say I have 2 apps: AppA has all the nice images I want to access from AppB. AppA has StreamProvider set like this:
<provider
android:authorities="org.example.AssetProvider"
android:name="com.commonsware.cwac.provider.StreamProvider"
android:exported="true"
android:grantUriPermissions="true">
<meta-data
android:name="com.commonsware.cwac.provider.STREAM_PROVIDER_PATHS"
android:resource="@xml/assetpaths" />
</provider>
with assetpaths.xml like this:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<asset name="assets" path="test"/>
</paths>
All works well and I can access my assets through StreamProvider in my AppA (getUriPrefix() for that authority returns some non-null value).
The difficult part begins when I try to access the assets from my AppB with this code:
String COMMON_AUTHORITY = "org.example.AssetProvider";
Uri test = Uri.parse("content://" + COMMON_AUTHORITY)
.buildUpon()
.appendPath(StreamProvider.getUriPrefix(COMMON_AUTHORITY))
.appendPath("assets")
.appendPath("test.jpg")
.build();
InputStream is = getContentResolver().openInputStream(test)
The getUriPrefix() method returns null as there is no Provider registered in INSTANCES private field of StreamProvider class. When I wrote down the prefix from running this code in AppA and then hard-coded it AppB, it all went well and I could obtain an InputStream.
My questions are -
Thank you for your reply in advance! :)
In AppA, subclass StreamProvider
and override getUriPrefix()
to return null
. Use the subclass in your <provider>
element. Then, build the Uri
in AppB without a prefix.
BTW, you should be able to remove android:grantUriPermissions="true"
, as you are exporting this provider.