2

I've a requirement to access database created by a system user from other service via a foreground user. Both services are part of the same application.

Created a system service, which creates a database and exposes it via ContentProvider. In another application, foreground user tries to access it via ContentResolver.

Added permissions

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERACT_ACROSS_USERS" />
<uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" />

manifest declarations

<service
android:name=".EXDBService"
android:enabled="true"
android:singleUser="true"
android:exported="true" />

<service
android:name=".EXService"
android:enabled="true"
android:exported="true"
android:permission="com.example.EX_SERVICE" />
<provider
android:name=".EXDBMananger"
android:authorities="com.example.provider"
android:exported="true"
android:readPermission="com.example.provider.READ_DATABASE"
android:writePermission="com.example.provider.WRITE_DATABASE" />

But when making the request like insert/update with Contentresolver, it is throwing an error couldn't open database.

ContentResolver resolver = getContentResolver();
Uri uri = DbContract.BASE_CONTENT_URI;
// initiate contentValues with valid data
Cursor cursor = resolver.insert(uri, contentValues);

Wish I could share more code here, but any guesses for what this could cause this issue?

I saw the user id 11 trying to access database on other application which is running on user 0. Does this cause any permission issue?

2023-04-20 21:01:48.745 8530-8530/com.example.xxxxx E/SQLiteDatabase: Failed to open database '/data/data/com.example.xxxxx/databases/My.DB'.
    android.database.sqlite.SQLiteCantOpenDatabaseException: Cannot open database '/data/data/com.example.xxxxx/databases/MY.DB': Directory /data/data/com.example.xxxxx/databases doesn't exist
        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:254)
        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:205)
        at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:505)
        at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:206)
        at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:198)
        at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:919)
        at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:899)
        at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:763)
        at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:752)
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:383)
        at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:340)
4
  • it is throwing an error couldn't open database. - please share the stacktrace Commented Apr 21, 2023 at 1:16
  • edited the question to add stacktrace. Directory /data/data/com.example.xxxxx/databases doesn't exist Actually Directory exists, looks like permission issue, not sure what permission that is. Commented Apr 21, 2023 at 2:05
  • chmod 777 /data/data/com.example.xxxxx/databases/* ? Commented Apr 21, 2023 at 2:29
  • Why would there be a permission issue when reading through contentresolver? Why we need to do this manual step to give permission to the db folder? Commented Apr 21, 2023 at 2:40

1 Answer 1

1

I had the issue with manifest tag sharedId, removing which resolved the issue. Also here's how the application looks like after the problem is solved. Hopefully this helps.

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.