File.delete() function works only first time then throws f noilet found for other images

huangapple go评论73阅读模式
英文:

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

&quot;/storage/emulated/0/Android/data/com.pratiksahu.dokify/&quot;

This is the function which is used to delete files

fun setupDeleteImageButtonListener() {
        deleteFileButton.setOnClickListener {
            if(selectedItemsImage.size&gt;0)
            selectedItemsImage.forEach {
                File(&quot;/storage/emulated/0/Android/data/com.pratiksahu.dokify/&quot;, it.path).delete()
                    .let { result -&gt;
                        Log.d(TAG_DELETE, it.path + &quot; &lt;-- $result&quot;)
                    }
            }
            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

File.delete() function works only first time then throws f noilet found for other images

答案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  = &quot;/storage/emulated/0/Android/data/com.pratiksahu.dokify/files/Pictures&quot;
        val directory = File(path)
        if (directory.exists())
        {
            val files: Array&lt;File&gt; = directory.listFiles()
            if (files.size &gt; 0) {
                Arrays.sort(files, Comparator.comparingLong(File::lastModified).reversed())
                val tempDocInfoList = ArrayList&lt;DocInfo&gt;()
                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(&quot;/storage/emulated/0/Android/data/com.pratiksahu.dokify/&quot;, it.path)

into 

File(it.path)

huangapple
  • 本文由 发表于 2020年9月27日 01:28:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/64080644.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定