MediaStore lost access to files after uninstalling the app or clearing its data
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 42/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Stale
- Domain
- mobile-dev
Research direction
Start with MediaStoreCompat.createMedia at MediaStoreCompat.kt:143 and reproduce both cases on Android 12 using the listed writeToConfig and readConfig flows. Check the behavior after uninstall/reinstall and after manually deleting the Downloads files; done means the existing file remains discoverable and the later write does not trigger SQLiteConstraintException.
Written by the indexing model from the issue text.
Description
Library version: 1.5.1
OS version: [Android 12]
Device model: [Emulator Pixel 5]
Describe the bug
I'm creating a txt file in Downloads folder with this code:
fun writeToConfig() {
val file = MediaStoreCompat.createDownload(
context,
FileDescription("config-test.txt", "", "text/plain"),
CreateMode.REUSE,
)
file?.openOutputStream(true).use {
it?.write("HelloWorld\n".toByteArray())
}
}
And I'm reading the file with this approach:
fun readConfig() {
val file = MediaStoreCompat.fromFileName(context, MediaType.DOWNLOADS, "config-test.txt")
file?.openInputStream()?.use {
val text = it.readBytes().toString(Charsets.UTF_8)
logD(text)
}
}
Everything works fine. But after I uninstall the app and reinstall it, when I call the readConfig() function, the file variable is null. and strange thing is that when i call the writeToConfig() function again, instead of reusing the same file it creates a new file with this name: "config-test (1).txt".
So after I saw this, I tried to retest the code and I deleted the files (""config-test.txt" and "config-test (1).txt"). After force stopping and relaunching the app I got an Exception: android.database.sqlite.SQLiteConstraintException.
After this exception you cant work with this code anymore and the only way to fix this crash is that you need to wipe data from your device settings.
So we have two bugs in here
To Reproduce
- Use
MediaStoreCompatto write/read a file in Downloads folder. - Unistall the app
- Install the app
- (Bug 1 appears)
- Go to Downloads folder and manually delete the file.
- Force stop the app
- launch the app again.
- (Bug 2 --> Crash)
Stacktrace
FATAL EXCEPTION: main
Process: app.source.getcontact, PID: 8835
java.lang.RuntimeException: Unable to create application com.test.myapp.DebugApp: android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: files._data (code 2067 SQLITE_CONSTRAINT_UNIQUE)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6730)
at android.app.ActivityThread.access$1500(ActivityThread.java:247)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2053)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7839)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
Caused by: android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: files._data (code 2067 SQLITE_CONSTRAINT_UNIQUE)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:178)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:142)
at android.content.ContentProviderProxy.insert(ContentProviderNative.java:557)
at android.content.ContentResolver.insert(ContentResolver.java:2193)
at android.content.ContentResolver.insert(ContentResolver.java:2155)
at com.anggrayudi.storage.media.MediaStoreCompat.createMedia(MediaStoreCompat.kt:143)
at com.anggrayudi.storage.media.MediaStoreCompat.createDownload(MediaStoreCompat.kt:34)
at com.test.myapp.ConfigCache.writeToConfig(ConfigCacheImpl.kt:37)
at com.test.myapp.DebugApp.onCreate(DebugApp.kt:7)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1211)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6725)
at android.app.ActivityThread.access$1500(ActivityThread.java:247)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2053)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7839)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
- Dominant language
- Kotlin
- Stars
- 867
- Forks
- 103
- PR merge metrics
- No merged PRs in 30d
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from anggrayudi/SimpleStorage
-
Difficulty 2/5 1-3 hours Newbie friendliness 73/100
anggrayudi/SimpleStorage#160 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
anggrayudi/SimpleStorage#159 ·
-
bug unreproducible
Difficulty 4/5 3-5 days Newbie friendliness 30/100
anggrayudi/SimpleStorage#107 · 10 comments ·
-
enhancement
Difficulty 4/5 3-5 days Newbie friendliness 25/100
anggrayudi/SimpleStorage#106 · 1 comment ·
-
enhancement
Difficulty 3/5 1-2 days Newbie friendliness 35/100
anggrayudi/SimpleStorage#94 · 8 comments ·
All issues in anggrayudi/SimpleStorage
Similar issues
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
modelcontextprotocol/kotlin-sdk#1088 ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
openwallet-foundation/multipaz#2040 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
ohs-foundation/kotlin-fhir#143 ·
-
Feature request
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
keiyoushi/extensions-source#19546 ·
Maintainers usually reply within 1 day
-
bug triage
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
d4rken-org/sdmaid-se#2820 ·
Maintainers usually reply within 1 day