如何解决如何从相对路径的MediaStore中读取文件?
在Android 10或更高版本中,我使用MediaStore将文件保存在“下载”共享存储中。 我用来保存文件的代码如下:
GlobalScope.launch {
val values = ContentValues().apply {
put(MediaStore.Downloads.DISPLAY_NAME,"file.txt")
put(MediaStore.Downloads.MIME_TYPE,"text/plain")
put(MediaStore.Downloads.IS_PENDING,1)
put(MediaStore.Downloads.RELATIVE_PATH,Environment.DIRECTORY_DOWNLOADS+ File.separator+"MyApp/SubDir")
}
val collection = MediaStore.Downloads.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
val item = contentResolver.insert(collection,values)!!
contentResolver.openFileDescriptor(item,"w",null).use {
FileOutputStream(it!!.fileDescriptor).use { outputStream ->
outputStream.write("test file".toByteArray())
outputStream.close()
}
}
values.clear()
values.put(MediaStore.Images.Media.IS_PENDING,0)
contentResolver.update(item,values,null,null)
现在我的文件保存在Downloads / myApp / subDir / file.txt
如果是这样,如果我不知道该文件的Uri但知道用于保存该文件的RELATIVE_PATH,该如何读取该文件?
解决方法
现在可以借助SimpleStorage:
val fileList = MediaStoreCompat.fromRelativePath(this,Environment.DIRECTORY_DOWNLOADS)
val singleFile = MediaStoreCompat.fromRelativePath(this,Environment.DIRECTORY_DOWNLOADS + "/MyApp/SubDir/","file.txt")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。