Accessing custom content provider from different app
Asked Answered
M

6

20

Hello i have created an android app that uses a custom content provider named CustomCP, it implements all methods and everything works fine while managing data inside the app, but when i try to access it from another app i keep getting an error of " Failed to find provider info for com.example.customcp.

I have declared my content provider in the manifest file of the first app as

<provider android:name="com.example.CustomCP"      android:authorities="com.example.customcp"/>

I try to call the provider in the second's application start up activity

public class app2 extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Uri kUri = Uri.parse("content://com.example.customcp/key");
        Cursor c = managedQuery(kUri, null, null, null, null);
}
}

So the question is simple , is it possible to access a custom content provider from multiple applications?

Matilda answered 19/4, 2011 at 9:23 Comment(4)
I understand that you are essentially missing the ContentResolver to access the ContentProvider of another application. You need to use ContentResolver instead of the managedQueryCloselipped
I tried Uri kUri = Uri.parse("content://com.example.customcp/key"); ContentProviderClient cr = getContentResolver().acquireContentProviderClient(kUri); try { Cursor c = cr.query(kUri, null, null, null, null); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } , but still getting the same error on logcat "failed to find provider"Matilda
Also in the first application where you are writing the ContentProvider you need to declare that in the manifest file ofthat application. did you do that?Closelipped
Yes, if you mean this part: '<provider android:name="com.example.CustomCP" android:authorities="com.example.customcp" android:grantUriPermissions="true"/>', it's on the first manifest.Matilda
P
18

Yes, it's possible to access a custom content provider from another app. Using your terminology we'll call the content provider CustomCP and the other app AppA. (AppA is the one that wants to access to the provider). This approach is proven to work:

  1. Specify the desired content provider (CustomCP) from within AppA by using a ContentProviderClient:

    Uri yourURI = Uri.parse("content://com.example.customcp/YourDatabase"); ContentProviderClient yourCR = getContentResolver().acquireContentProviderClient(yourURI);

  2. Access the content provider as you would normally from App A. For example:

    yourCursor = yourCR.query(yourURI, null, null, null, null);

    Note: you must either enclose the code within a try/catch block or include a "throws RemoteException" since the provider is not in App A.

  3. CustomCP's Manifest must specify the provider, include the permissions allowed (e.g., read and/or write), and the provider must be exported. Here's an example:

    <provider
        android:name="your.package.contentprovider.YourProvider"
        android:authorities="YourAuthority"
        android:readPermission="android.permission.permRead"
        android:exported="true" >
     </provider>
    
Pinnacle answered 24/1, 2013 at 19:59 Comment(1)
Content Providers exported by default as far as I know. So you don't have to declare it in the manifest. Plus, if you need to use only the read permission of CP, you have to grant it from the accesser application side.Pentavalent
C
3

You can use below code to get the content provider data in other app.. You need to use content resolver to fetch the data..

try {
    //URL provided in first app while creating the content provider
    val URL = "content://com.test.localstorage.MyContentProvider/authentication"
    val data = Uri.parse(URL)
    val cursor: Cursor? = contentResolver.query(data, null, null, null, "user_id")

    if (cursor != null) {
        if (cursor.moveToFirst()) {
            do {
                return cursor.getString(cursor.getColumnIndex("user_id")).toString()
            } while (cursor.moveToNext())
        }
    }
} catch (ex: Exception) {
    ex.printStackTrace()
}
Carine answered 16/5, 2020 at 16:16 Comment(0)
B
2

I know I am late, but just in case someone stumbles upon this question, I think this is the accepted answer. This one saved my day. So in this case, we have CustomCP in one app. Say, the other app is called AppDemo2. Then first define a permission in CustomCPs AndroidManifest.xml:

<permission android:name="com.any_string.MY_PERMISSION_FOR_SOMETHING"/>
<application ...
...></application>

Next you have to create a provider:

<application ... >
<provider
            android:authorities="com.package_name_where_customCP_is"
            android:name=".CustomCP"
            android:readPermission="com.any_string.MY_PERMISSION_FOR_SOMETHING"
            android:enabled="true"
            android:exported="true"
            android:multiprocess="true"
            >
        </provider>
<activity...>

Finally, in your 2nd app (AppDemo2 in my answer), open up its manifest file and add this permission:

<uses-permission android:name="com.any_string.MY_PERMISSION_FOR_SOMETHING"/>

This is the answer that worked for me. I hope this helps :)

Brezhnev answered 29/10, 2019 at 9:19 Comment(0)
G
1
 The custom content provider should have the permission 

  <provider
       android:name="pakageName.ProviderClassName"
       android:authorities="pakageName.ProviderClassName"
       android:grantUriPermissions="true"
       android:exported="true"
       android:multiprocess="true" >
  </provider>
Goggleeyed answered 26/8, 2021 at 10:55 Comment(0)
T
0

in the manifest file, make sure that your

"provider android..>"
is inside your
 "application .. /application>" 

hope that helps

Tigress answered 30/11, 2011 at 11:14 Comment(0)
A
0

After creating the content provider , specify the content provider in the manifest file. You can mention content provider using the tag. Inside the provider tag dont forget to mention the name and authorities attributes. This declaration should be ..

<provider
        android:name="pakgName.ProviderClassName"
        android:authorities="pakgName.ProviderClassName"
        android:multiprocess="true" >
    </provider>

Here what you mention in the authorities attribute that should be match when you try to get the data from the provider.

Atharvaveda answered 20/3, 2013 at 11:37 Comment(2)
@PeteH, Your answer is good (+1). But i didn't understand significance of android:exported="true" in 4th step.Atharvaveda
why android:multiprocess="true" is required here ?Eisele

© 2022 - 2024 — McMap. All rights reserved.