7

I have to delete folder with more than 30000 images. I have '.nomedia' file inside to prevent scanning all the time. Trying to delete all files I have to check file name for every file. If I miss this check - '.nomedia' file will be deleted. If this happens before images - this will cause great performance loss. Any idea how to solve this?

UPDATE:

Does anybody know way to hide images from scanning without use '.nomedia'? I can save files with fake extensions, but I'm not sure is this working.

Update:

Actually test shows me this result: Check for name makes deleting about 50% slower. Problem is not in check code but in system scanning folder :(

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Kostadin
  • 2,499
  • 5
  • 34
  • 58
  • "I have '.nomedia' file inside to prevent scanning all the time." - What is scanning all the time? There shouldn't be anything running on an Android device which constantly scans media folders. – Squonk Mar 17 '12 at 07:47
  • 1
    I'm adding images for map cache. When I drag map - about 10-15 for every drag. If android scan after all new image in folder - this is disaster. – Kostadin Mar 17 '12 at 08:11
  • 1
    @MisterSquonk: Some companies, like Samsung and Sony, have system apps that scans all media folders and build some sort of a library over all media that exists on the device. – bos Mar 17 '12 at 10:17
  • @MisterSquonk my case is same. I'll try to use unknown file extensions. – Kostadin Mar 17 '12 at 10:20
  • you can consider running "rm *" (does not delete things that starts with a '.' in a java.lang.Process – njzk2 Mar 28 '12 at 13:53

2 Answers2

2

My solution for moment is:

  1. Rename folder with images.
  2. Create new folder for images with '.nomedia' file inside.
  3. Start background thread to delete old folder.

This way is not faster than previous but allows user to continue work immediately. Of course with one presumption - user have to know about deleting and cleaning occupied space is not finished for a while.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Kostadin
  • 2,499
  • 5
  • 34
  • 58
  • Pretty clever solution actually. To speed up the actual delete process, take a look at this answer for using `rm -rf`: https://stackoverflow.com/a/58420953/293280 We found it to be multiple times faster than using `FileUtils`. – Joshua Pinter Oct 16 '19 at 20:07
0

Are you sure that the process of checking file name alone takes significantly long time? In order to delete the files, I guess you would need the File object anyway. Having the File object already, what is the performance cost of doing the below?

".nomedia".equals(file.getName());

The cost of actually deleting (file.delete()) should be orders of magnitude more than a string comparison.

Have you measured the actual time taken to check file names?

Dheeraj Vepakomma
  • 26,870
  • 17
  • 81
  • 104
  • How did you conduct the test? What is the cumulative time taken for 30000 string comparisons? – Dheeraj Vepakomma Mar 17 '12 at 08:17
  • 1
    Actually test shows me this result: Check for name makes deleting about 50% slower. Problem is not in check code but in system scanning folder :( – Kostadin Mar 17 '12 at 08:25