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

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

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

问题

我将所有文件的 URI 存储在一个列表中,这些文件位于目录中:

  1. "/storage/emulated/0/Android/data/com.pratiksahu.dokify/"

以下是用于删除文件的函数:

  1. fun setupDeleteImageButtonListener() {
  2. deleteFileButton.setOnClickListener {
  3. if(selectedItemsImage.size > 0)
  4. selectedItemsImage.forEach {
  5. File("/storage/emulated/0/Android/data/com.pratiksahu.dokify/", it.path).delete()
  6. .let { result ->
  7. Log.d(TAG_DELETE, it.path + " <-- $result")
  8. }
  9. }
  10. selectedItemsImage.clear() //使用此操作存储图像 URI
  11. selectedItems.clear() //使用此操作存储在带有复选框的 RecyclerView 中使用的索引
  12. importedImagesAdapter?.setSelectedItems(selectedItems) //更新复选框选择的项目
  13. imageList.clear() //清除从应用程序启动时扫描目录中检索的列表
  14. imagePagerViewModel.initImages() //刷新可用文件列表(包含 URI)
  15. importedImagesAdapter?.items = imageList //将新列表设置到适配器中
  16. }
  17. }

我能够在第一批中删除文件,但是如果我尝试在第二批中删除文件,File.delete() 将无法找到文件,尽管该文件位于目录中。

英文:

I am storing uri for all files in a list which are in
Directory

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

This is the function which is used to delete files

  1. fun setupDeleteImageButtonListener() {
  2. deleteFileButton.setOnClickListener {
  3. if(selectedItemsImage.size&gt;0)
  4. selectedItemsImage.forEach {
  5. File(&quot;/storage/emulated/0/Android/data/com.pratiksahu.dokify/&quot;, it.path).delete()
  6. .let { result -&gt;
  7. Log.d(TAG_DELETE, it.path + &quot; &lt;-- $result&quot;)
  8. }
  9. }
  10. selectedItemsImage.clear() //using this to store image uri
  11. selectedItems.clear() //using this to store index which is being used in recyclerview with checkbox
  12. importedImagesAdapter?.setSelectedItems(selectedItems) //updating checkbox selected items
  13. imageList.clear() //Clearing list which is retrieved from scanning the directory when app starts
  14. imagePagerViewModel.initImages() // Refreshing available files list (Contains uri)
  15. importedImagesAdapter?.items = imageList //Setting new list to adapter
  16. }
  17. }

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。

  1. fun initImages() {
  2. _loading.value = true
  3. val path = "/storage/emulated/0/Android/data/com.pratiksahu.dokify/files/Pictures"
  4. val directory = File(path)
  5. if (directory.exists()) {
  6. val files: Array<File> = directory.listFiles()
  7. if (files.size > 0) {
  8. Arrays.sort(files, Comparator.comparingLong(File::lastModified).reversed())
  9. val tempDocInfoList = ArrayList<DocInfo>()
  10. for (i in files.indices) {
  11. val tempDocInfo = DocInfo(files[i].toUri(), files[i].name, files[i].length().toString())
  12. tempDocInfoList.add(tempDocInfo)
  13. }
  14. setImage(tempDocInfoList)
  15. }
  16. }
  17. _loading.value = false
  18. }

不同于其他情况,如果我尝试从图库或相机导入,其中就不会有完整的路径。所以我代码中唯一需要更改的地方是:

  1. File("/storage/emulated/0/Android/data/com.pratiksahu.dokify/", it.path)
  2. 改为
  3. File(it.path)
英文:

Found the error , I was scanning the directory and storing the uri which had the full path in it

  1. fun initImages() {
  2. _loading.value = true
  3. val path = &quot;/storage/emulated/0/Android/data/com.pratiksahu.dokify/files/Pictures&quot;
  4. val directory = File(path)
  5. if (directory.exists())
  6. {
  7. val files: Array&lt;File&gt; = directory.listFiles()
  8. if (files.size &gt; 0) {
  9. Arrays.sort(files, Comparator.comparingLong(File::lastModified).reversed())
  10. val tempDocInfoList = ArrayList&lt;DocInfo&gt;()
  11. for (i in files.indices) {
  12. val tempDocInfo = DocInfo(files[i].toUri() , files[i].name , files[i].length().toString())
  13. tempDocInfoList.add(tempDocInfo)
  14. }
  15. setImage(tempDocInfoList)
  16. }
  17. }
  18. _loading.value = false
  19. }

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

  1. File(&quot;/storage/emulated/0/Android/data/com.pratiksahu.dokify/&quot;, it.path)
  2. into
  3. 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:

确定