英文:
File.delete() function works only first time then throws f noilet found for other images
问题
我将所有文件的 URI 存储在一个列表中,这些文件位于目录中:
"/storage/emulated/0/Android/data/com.pratiksahu.dokify/"
以下是用于删除文件的函数:
fun setupDeleteImageButtonListener() {
deleteFileButton.setOnClickListener {
if(selectedItemsImage.size > 0)
selectedItemsImage.forEach {
File("/storage/emulated/0/Android/data/com.pratiksahu.dokify/", it.path).delete()
.let { result ->
Log.d(TAG_DELETE, it.path + " <-- $result")
}
}
selectedItemsImage.clear() //使用此操作存储图像 URI
selectedItems.clear() //使用此操作存储在带有复选框的 RecyclerView 中使用的索引
importedImagesAdapter?.setSelectedItems(selectedItems) //更新复选框选择的项目
imageList.clear() //清除从应用程序启动时扫描目录中检索的列表
imagePagerViewModel.initImages() //刷新可用文件列表(包含 URI)
importedImagesAdapter?.items = imageList //将新列表设置到适配器中
}
}
我能够在第一批中删除文件,但是如果我尝试在第二批中删除文件,File.delete()
将无法找到文件,尽管该文件位于目录中。
英文:
I am storing uri for all files in a list which are in
Directory
"/storage/emulated/0/Android/data/com.pratiksahu.dokify/"
This is the function which is used to delete files
fun setupDeleteImageButtonListener() {
deleteFileButton.setOnClickListener {
if(selectedItemsImage.size>0)
selectedItemsImage.forEach {
File("/storage/emulated/0/Android/data/com.pratiksahu.dokify/", it.path).delete()
.let { result ->
Log.d(TAG_DELETE, it.path + " <-- $result")
}
}
selectedItemsImage.clear() //using this to store image uri
selectedItems.clear() //using this to store index which is being used in recyclerview with checkbox
importedImagesAdapter?.setSelectedItems(selectedItems) //updating checkbox selected items
imageList.clear() //Clearing list which is retrieved from scanning the directory when app starts
imagePagerViewModel.initImages() // Refreshing available files list (Contains uri)
importedImagesAdapter?.items = imageList //Setting new list to adapter
}
}
I am able to delete files in first batch , but if I try to delete files in second batch , File.delete() is not able to find the file though it is present in directory
答案1
得分: 0
发现错误,我在扫描目录并存储带有完整路径的URI。
fun initImages() {
_loading.value = true
val path = "/storage/emulated/0/Android/data/com.pratiksahu.dokify/files/Pictures"
val directory = File(path)
if (directory.exists()) {
val files: Array<File> = directory.listFiles()
if (files.size > 0) {
Arrays.sort(files, Comparator.comparingLong(File::lastModified).reversed())
val tempDocInfoList = ArrayList<DocInfo>()
for (i in files.indices) {
val tempDocInfo = DocInfo(files[i].toUri(), files[i].name, files[i].length().toString())
tempDocInfoList.add(tempDocInfo)
}
setImage(tempDocInfoList)
}
}
_loading.value = false
}
不同于其他情况,如果我尝试从图库或相机导入,其中就不会有完整的路径。所以我代码中唯一需要更改的地方是:
File("/storage/emulated/0/Android/data/com.pratiksahu.dokify/", it.path)
改为
File(it.path)
英文:
Found the error , I was scanning the directory and storing the uri which had the full path in it
fun initImages() {
_loading.value = true
val path = "/storage/emulated/0/Android/data/com.pratiksahu.dokify/files/Pictures"
val directory = File(path)
if (directory.exists())
{
val files: Array<File> = directory.listFiles()
if (files.size > 0) {
Arrays.sort(files, Comparator.comparingLong(File::lastModified).reversed())
val tempDocInfoList = ArrayList<DocInfo>()
for (i in files.indices) {
val tempDocInfo = DocInfo(files[i].toUri() , files[i].name , files[i].length().toString())
tempDocInfoList.add(tempDocInfo)
}
setImage(tempDocInfoList)
}
}
_loading.value = false
}
Unlike in other scenarios where If I try to import from gallery or camera it won't have full path in it.
So all I had to change in my code was
File("/storage/emulated/0/Android/data/com.pratiksahu.dokify/", it.path)
into
File(it.path)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论