Ali
Ali

Reputation: 529

Duplicate Files on phone's storage (Android)

Hope you are fine what I am trying to achieve is to get all the duplications from the internal storage, for that, I am already using a method I don't know what I am doing wrong please have a look,

private fun getDuplicateItems() {
    var tempDuplicateItem: DuplicateItemModel
    GlobalScope.launch(Dispatchers.IO) {
        for (i in 0 until listOfAllFiles.size) {
            for (j in i until listOfAllFiles.size) {
                if(!File(listOfAllFiles[i].directoryPath).isDirectory && !File(listOfAllFiles[j].directoryPath).isDirectory ){
                    if (listOfAllFiles[i].fileName == listOfAllFiles[j].fileName && listOfAllFiles[i].fileSize == listOfAllFiles[j].fileSize &&
                                    !listOfAllFiles[i].fileName.endsWith(".tmp") && !listOfAllFiles[j].fileName.endsWith(".tmp") &&
                                    !listOfAllFiles[i].fileName.endsWith(".chck") && !listOfAllFiles[j].fileName.endsWith(".chck") &&
                                    !listOfAllFiles[i].fileName.startsWith(".") && !listOfAllFiles[j].fileName.startsWith(".")
                    ) {
                        tempDuplicateItem = DuplicateItemModel(
                                "$i${listOfAllFiles[i].fileName}",
                                listOfAllFiles[i].fileName,
                                listOfAllFiles[i].absolutePath,
                                listOfAllFiles[i].fileModificationDateAndTime,
                                File(listOfAllFiles[i].directoryPath).length(),
                                false,
                                false,
                        )
                        tempArrayListOfDuplicateItemsSingle.add(
                                DuplicateItemModel(
                                        "$i${listOfAllFiles[i].fileName}",
                                        listOfAllFiles[j].fileName,
                                        listOfAllFiles[j].absolutePath,
                                        listOfAllFiles[j].fileModificationDateAndTime,
                                        File(listOfAllFiles[j].directoryPath).length(),
                                        true,
                                        true,
                                )
                        )

                        tempArrayListOfDuplicateItems.add(
                                DuplicateItemArrayListItem(
                                        tempDuplicateItem,
                                        tempArrayListOfDuplicateItemsSingle
                                )
                        )
                    }
                }
            }
        }
    }.invokeOnCompletion {
        var tempTotalDuplicateSize: Double = 0.0
        GlobalScope.launch(Dispatchers.Main) {
            progressBarDuplicateFiles.visibility = View.INVISIBLE
            imageViewNextDuplicateFiles.visibility = View.VISIBLE

            for (x in 0 until tempArrayListOfDuplicateItems.size) {
                tempArrayListOfDuplicateItems[x].listOfDuplicationSubItems.forEach { duplicateItem ->
                    tempTotalDuplicateSize += duplicateItem.fileSize
                }
            }

              //UI Stuff

            textViewTotalSizeOfDuplications.text = "total Size of Duplicate Files (${HelperClass.readableFileSize(tempTotalDuplicateSize.toLong())})"
            textViewTotalFolderFilesDuplicateItem.text = "${tempArrayListOfDuplicateItems.size} Item(s)"
            relativeLayoutDuplicateItem.isClickable = true
        }
    }
}

I am using MediaLoader, a library which gives me all the files on internal storage, and through those files, I am checking the data if the data is equal or not but getting the stream of data which I don't know is correct or not,

Upvotes: 0

Views: 532

Answers (1)

Dan Baruch
Dan Baruch

Reputation: 1093

This should help you get started:

File f = new File("your_relevant_path");
File[] files = f.listFiles();
HashSet<String> mHashSet = new HashSet<>();
   for (File currFile :
           files) {
       if (!mHashSet.add(currFile.getName()))
       {
            // Here there is a duplication of a file
       }
   }

Upvotes: 1

Related Questions