Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

MediaStore lost access to files after uninstalling the app or clearing its data

Aperta
#103 7 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
42/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
android, kotlin
Ambito
mobile-dev

Direzione di ricerca

Inizia da MediaStoreCompat.createMedia in MediaStoreCompat.kt:143 e riproduci entrambi i casi su Android 12 usando i flussi writeToConfig e readConfig elencati. Verifica il comportamento dopo la disinstallazione/reinstallazione e dopo l’eliminazione manuale dei file di Downloads; il lavoro è completato quando il file esistente rimane individuabile e la scrittura successiva non genera SQLiteConstraintException.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug wontfix

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

  1. Use MediaStoreCompat to write/read a file in Downloads folder.
  2. Unistall the app
  3. Install the app
  4. (Bug 1 appears)
  5. Go to Downloads folder and manually delete the file.
  6. Force stop the app
  7. launch the app again.
  8. (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) 
Lingua principale
Kotlin
Stelle
867
Fork
103
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di anggrayudi/SimpleStorage

Tutte le issue di anggrayudi/SimpleStorage

Issue simili

Altre issue su Kotlin

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.