英文:
Is there a way to use SAF to access txt files in the same directory as images in an Android app?
问题
我尝试加载我的应用中的图像,按照我尝试导入的格式,每个图像都有一个对应的标签文件。例如,cat.jpg 有一个相应的 cat.txt 文件,其中可能包含 "猫,超重,Felix,家"。
我获取了一个文件 URI 列表,将其添加到图片列表中,加载这些图片时,我会检查文本文件是否存在,如果存在,就将其添加到图片的标签列表中。
所有我尝试访问文本文件的尝试都没有成功,似乎始终存在 URI 的问题。
我还尝试获取包含图片文件的文件夹,并列出其中的文件,但也没有成功。
我猜想处理这种类型的数据可能不太符合 SAF 方法,但最好保留这种结构(图像 + txt 标签)以便与其他系统交互。
我使用 Android API 30 来测试应用,如果这有关联的话。以下是我编写的代码,它总是导致文件未找到:
fun getPictures(fileUris: List<Uri>): List<Picture> {
val picturesList = mutableListOf<Picture>()
for (fileUri in fileUris) {
val picture = Picture(fileUri, mutableListOf())
// 生成与图片文件对应的 txt 文件名
val imageNameWithoutExtension = fileUri.lastPathSegment?.substringBeforeLast(".")
val txtName = "$imageNameWithoutExtension.txt"
val documentId = DocumentsContract.getDocumentId(fileUri)
val parentDocumentId = documentId.substringBeforeLast(":")
val txtDocumentId = "$parentDocumentId:$txtName"
val txtFileUri = DocumentsContract.buildDocumentUriUsingTree(fileUri, txtDocumentId)
val documentFile = DocumentFile.fromSingleUri(context, txtFileUri)
var mimeType: String? = null
if (documentFile != null && documentFile.exists()) {
// 文件存在,现在可以获取 MIME 类型
mimeType = documentFile.type
} else {
Log.d("MSG", "文件未找到")
}
// 如果 .txt 文件存在且其 MIME 类型为文本,从中读取标签
if (mimeType != null && isTextMimeType(mimeType)) {
val tags = readTags(Picture(txtFileUri, mutableListOf()))
picture.tags = tags
}
picturesList.add(picture)
}
// 返回图片列表
return picturesList
}
如果您需要进一步的帮助或翻译,请告诉我。
英文:
I'm trying to load images in my app, in the format in trying to import, each image has a corresponding tag file. For instance cat.jpg has a corresponding cat.txt find that could contain "Cat, overweight, Felix, Home".
I get a list of file URI, that I'm adding to a list of pictures, and while loading those pictures, I'm checking for the existence of a text file, and it exists I add it to the list of tags of the picture.
All my attempts to access text files have been unsuccessful, it seems that there is always an issue with the URI of those.
I also tried to get the folder that contains the picture file, and list the files in it, but that was also unsuccessful.
I guess that dealing with this type of data is not very compliant with the SAF approach, but it would be better to keep this structure (image + tags in txt) in order to interact with other systems.
I'm using Android API 30 to test the app, if that is if any relevance.
Here is the code I came with, it always result in file not found:
fun getPictures(fileUris: List<Uri>): List<Picture> {
val picturesList = mutableListOf<Picture>()
for (fileUri in fileUris) {
val picture = Picture(fileUri, mutableListOf())
// Generate a txt file name corresponding to the picture file
val imageNameWithoutExtension = fileUri.lastPathSegment?.substringBeforeLast(".")
val txtName = "$imageNameWithoutExtension.txt"
val documentId = DocumentsContract.getDocumentId(fileUri)
val parentDocumentId = documentId.substringBeforeLast(":")
val txtDocumentId = "$parentDocumentId:$txtName"
val txtFileUri = DocumentsContract.buildDocumentUriUsingTree(fileUri, txtDocumentId)
val documentFile = DocumentFile.fromSingleUri(context, txtFileUri)
var mimeType: String? = null
if (documentFile != null && documentFile.exists()) {
// File exists, now you can get the MIME type
mimeType = documentFile.type
}else{
Log.d("MSG", "file not found")
}
// If the .txt file exists and its mime type is text, read the tags from it
if (mimeType != null && isTextMimeType(mimeType)) {
val tags = readTags(Picture(txtFileUri, mutableListOf()))
picture.tags = tags
}
picturesList.add(picture)
}
答案1
得分: 0
以下是您要翻译的内容:
"Actually, getting the string of fileUri, and replace the extension with a 'txt' is much a simpler and works.
I don't know if it always generates the correct URI, but that's the only way I got it working until now.
fun getPictures(fileUris: List
val picturesList = mutableListOf
for (fileUri in fileUris) {
val picture = Picture(fileUri, mutableListOf())
val txtFileUriString = fileUri.toString().substringBeforeLast(".") + ".txt"
val txtFileUri = Uri.parse(txtFileUriString)
val documentFile = DocumentFile.fromSingleUri(context, txtFileUri)
var mimeType: String? = null
if (documentFile != null && documentFile.exists()) {
// File exists, now you can get the MIME type
mimeType = documentFile.type
} else {
Log.d("MSG", "file not found")
}
// If the .txt file exists and its mime type is text, read the tags from it
if (mimeType != null && isTextMimeType(mimeType)) {
val tags = readTags(Picture(txtFileUri, mutableListOf()))
picture.tags = tags
}
picturesList.add(picture)
}
return picturesList
}"
请注意,我已经将代码中的HTML转义字符解码为正常文本。
英文:
Actually, getting the string of fileUri, and replace the extension with a "txt" is much a simpler and works.
I don't know if it always generate correct URI, but that's the only way I got it working until now.
fun getPictures(fileUris: List<Uri>): List<Picture> {
val picturesList = mutableListOf<Picture>()
for (fileUri in fileUris) {
val picture = Picture(fileUri, mutableListOf())
val txtFileUriString = fileUri.toString().substringBeforeLast(".") + ".txt"
val txtFileUri = Uri.parse(txtFileUriString)
val documentFile = DocumentFile.fromSingleUri(context, txtFileUri)
var mimeType: String? = null
if (documentFile != null && documentFile.exists()) {
// File exists, now you can get the MIME type
mimeType = documentFile.type
} else {
Log.d("MSG", "file not found")
}
// If the .txt file exists and its mime type is text, read the tags from it
if (mimeType != null && isTextMimeType(mimeType)) {
val tags = readTags(Picture(txtFileUri, mutableListOf()))
picture.tags = tags
}
picturesList.add(picture)
}
return picturesList
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论