访问由我的应用程序创建的文件
我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案是创建一个子文件夹并将我的文件保存在那里。为此,我在
>= 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
andMediaStore.Audio.Media.DATA
on other version of Android.