For getting all images on an device I query the ContentResolver
for MediaStore.Images
. Now I want to add a option to show hidden files as well like many image apps like QuickPic do it.
Is there a faster way than recursively searching all directories on the phone and check if a .nomedia file is in it and if so, check if I can find some image file in it?
It's not possible through the ContentResolver
is it?
You can hide files in your Safe folder and control access with a PIN. Tip: This feature is available for Android 8.0 and up. Important: If you forget your PIN or Pattern, you can't access your files.
In Android, there's no option in the Settings app to show or hide files. But you can install file manager apps to view hidden files. Astro File Manager is one such app that works very well.
I am adding another Answer which is
ContentResolver
to scan all hidden Folders containing .noMedia
file and then check if that folder have any Image file. You can modify code easily to return list of hidden image files as well. I made a dummy app to test my code and here is output
You can see test App is showing WhatsApp's Sent Images
folder have hidden images but at same time Video
folder have none (as per requirement).
How to do this
Problem can be divide into to parts
1) First of all with the help of content resolver
get all Directories with .noMedia
file in them . The code snippet below is self explanatory
private static final String FILE_TYPE_NO_MEDIA = ".nomedia";
/**
* This function return list of hidden media files
*
* @param context
* @return list of hidden media files
*/
private ArrayList<CustomFile> filterFiles(Context context) {
ArrayList<CustomFile> listOfHiddenFiles = new ArrayList<CustomFile>();
String hiddenFilePath;
// Scan all no Media files
String nonMediaCondition = MediaStore.Files.FileColumns.MEDIA_TYPE
+ "=" + MediaStore.Files.FileColumns.MEDIA_TYPE_NONE;
// Files with name contain .nomedia
String where = nonMediaCondition + " AND "
+ MediaStore.Files.FileColumns.TITLE + " LIKE ?";
String[] params = new String[] { "%" + FILE_TYPE_NO_MEDIA + "%" };
// make query for non media files with file title contain ".nomedia" as
// text on External Media URI
Cursor cursor = context.getContentResolver().query(
MediaStore.Files.getContentUri("external"),
new String[] { MediaStore.Files.FileColumns.DATA }, where,
params, null);
// No Hidden file found
if (cursor.getCount() == 0) {
listOfHiddenFiles.add(new CustomFile("No Hidden File Found",
"Nothing to show Here", "Nothing to show Here", false));
// show Nothing Found
return listOfHiddenFiles;
}
// Add Hidden file name, path and directory in file object
while (cursor.moveToNext()) {
hiddenFilePath = cursor.getString(cursor
.getColumnIndex(MediaStore.Files.FileColumns.DATA));
if (hiddenFilePath != null) {
listOfHiddenFiles
.add(new CustomFile(FileUtils
.getFileName(hiddenFilePath), hiddenFilePath,
FileUtils.getFileParent(hiddenFilePath),
isDirHaveImages(FileUtils
.getFileParent(hiddenFilePath))));
}
}
cursor.close();
return listOfHiddenFiles;
}
2) Now for second part of puzzle How to find if directory containing .noMedia
file have hidden images in it.
Solution is use simple for loop to check if any of file in directory have image file extension(.jpg,.png etc), if that is true break the loop and set flag that, current directory have some hidden images in it.
Here instead of breaking for loop
you can return list of images using function from my first Answer.**
/**
*
* @param dir
* to serch in
* @param fileType
* //pass fileType as a music , video, etc.
* @return ArrayList of files of comes under fileType cataegory
*/
public boolean isDirHaveImages(String hiddenDirectoryPath) {
File listFile[] = new File(hiddenDirectoryPath).listFiles();
boolean dirHaveImages = false;
if (listFile != null && listFile.length > 0) {
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].getName().endsWith(".png")
|| listFile[i].getName().endsWith(".jpg")
|| listFile[i].getName().endsWith(".jpeg")
|| listFile[i].getName().endsWith(".gif")) {
// Break even if folder have a single image file
dirHaveImages = true;
break;
}
}
}
return dirHaveImages;
}
Now back to your question
Is there a faster way than recursively searching all directories on
the phone and check if a.nomedia
file is in it?
See code snippet 1
-
and if so, check if I can find some image file in it?
See code snippet 2
It's not possible through the
ContentResolver
is it?
yes it is !!
I have uploade entire sample on Github, you can download and Modify as you wish
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With