访问由我的应用程序创建的文件

发布于 2025-01-17 03:45:45 字数 1686 浏览 3 评论 0原文

我正在使用 MediaStore 在 Android 上创建文件,然后我需要访问它们进行读取。我该怎么做?

有没有办法查询我的所有文件,或者通过 ContentResolver 按应用程序所有者以某种方式过滤它们,或者我可以在创建时标记我的文件,以便稍后按此标记过滤它们?


我的代码:

val resolver: ContentResolver = /* Is passed as an argument */
val collection = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
    MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
} else MediaStore.Audio.Media.EXTERNAL_CONTENT_URI

fun createFile(): Uri? {
    val fileDetails = ContentValues().apply {
        put(MediaStore.Audio.Media.DISPLAY_NAME, "example.aac")
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
            put(MediaStore.Audio.Media.IS_PENDING, 1)
    }

    return resolver.insert(collection, fileDetails)
}

/* Between calling these two functions I also set `IS_PENDING` to 0 */

fun findRecordings(): List<MyFile> {
    val projection = arrayOf(
        MediaStore.Audio.Media._ID,
        /* ... */
    )
    val selection = /* Filter to only get the files created by my app */
    val selectionArgs = /* ... */
    val sortOrder = /* ... */

    val files = mutableListOf<MyFile>()

    resolver.query(collection, projection, selection, selectionArgs, sortOrder)?.use { cursor ->
        val idColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID)
        /* Other columns */

        while (cursor.moveToNext()) {
            val id = cursor.getLong(idColumn)
            /* Other data */

            val uri = ContentUris.withAppendedId(collection, id)

            files += MyFile(uri, /* ... */)
        }
    }

    return files
}

I am using MediaStore to create files on Android, then I need to access them for reading. How can I do this?

Is there a way to query all my files, or filter them by application owner through ContentResolver somehow, or maybe I can mark my files at the creation time to filter them by this mark later?


My code:

val resolver: ContentResolver = /* Is passed as an argument */
val collection = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
    MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
} else MediaStore.Audio.Media.EXTERNAL_CONTENT_URI

fun createFile(): Uri? {
    val fileDetails = ContentValues().apply {
        put(MediaStore.Audio.Media.DISPLAY_NAME, "example.aac")
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
            put(MediaStore.Audio.Media.IS_PENDING, 1)
    }

    return resolver.insert(collection, fileDetails)
}

/* Between calling these two functions I also set `IS_PENDING` to 0 */

fun findRecordings(): List<MyFile> {
    val projection = arrayOf(
        MediaStore.Audio.Media._ID,
        /* ... */
    )
    val selection = /* Filter to only get the files created by my app */
    val selectionArgs = /* ... */
    val sortOrder = /* ... */

    val files = mutableListOf<MyFile>()

    resolver.query(collection, projection, selection, selectionArgs, sortOrder)?.use { cursor ->
        val idColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID)
        /* Other columns */

        while (cursor.moveToNext()) {
            val id = cursor.getLong(idColumn)
            /* Other data */

            val uri = ContentUris.withAppendedId(collection, id)

            files += MyFile(uri, /* ... */)
        }
    }

    return files
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好多鱼好多余 2025-01-24 03:45:45

解决方案是创建一个子文件夹并将我的文件保存在那里。为此,我在 >= Build.VERSION_CODES.Q 上使用了 MediaStore.Audio.Media.RELATIVE_PATH ,在其他版本的Android。

The solution was to create a subfolder and save my files there. For that I used MediaStore.Audio.Media.RELATIVE_PATH on >= Build.VERSION_CODES.Q and MediaStore.Audio.Media.DATA on other version of Android.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文