tr4n
tr4n

Reputation: 122

How to remove LOST.DIR folder from USB Mass Storage in Android programmatically

I copy files from Android internal storage to USB drive, folder LOST.DIR always is created by system. How can I remove this folder programmatically

I use library libaums to handle communicate with USB I tried to delete this folder after copy but USB get error

    private fun exportOnlyFilesToUsb(fileSystem: FileSystem, file: File) {
        val root = fileSystem.rootDirectory
        if (!file.isDirectory) {
            root.search(file.name)?.delete()
            val targetFile = root.createFile(file.name)
            copyFile(file, targetFile)
            return
        }
        file.listFiles()?.forEach {
            exportOnlyFilesToUsb(fileSystem, it)
        }
    }

    private fun copyFile(file: File, usbFile: UsbFile) {
        if (file.isDirectory || usbFile.isDirectory) return
        FileInputStream(file).use { input ->
            UsbFileOutputStream(usbFile).use { output ->
                input.copyTo(output)
            }
        }
    }

// Then I delete 
root.search(LOST_DIR)?.delete()

Upvotes: 1

Views: 2273

Answers (2)

blackapps
blackapps

Reputation: 9292

First check if you have write access with File.canWrite().

I think you have no write access to removable media since Android KitKat.

You would need SAF and let the user select the directory with ACTION_OPEN_DOCUMENT_TREE.

Yes it is a nuisance as the medium will be used in other devices which sometimes dont like that directory.

The same counts for an Android directory that is often created.

Yes remove all. Nobody asked for it.

Upvotes: 1

Vijay
Vijay

Reputation: 1388

LOST.DIR is a folder created by an Android phone on the SD card to get back accidentally deleted files like photos, videos, etc. If you are a smartphone user, you would have already seen LOST.DIR folders are located on your external SD card.

Lost.DIR folder basically helps you to recover files lost due to various reasons like application crash, improper ejection of the USB drive or SD card/ ejection of SD card during the reading/ writing process, Android abruptly frozen or shut down unexpectedly, interruptions while downloading files on SD card, etc.

Some users might delete Lost.DIR folder thinking that it is a virus folder, which is completely false. You might have observed that once you delete Lost.DIR folder, again it will be automatically recreated after you boot your phone, this is because LOST.DIR is the Android system folder.

So don't delete this. Actually, you can't do that.

Upvotes: 1

Related Questions