I am working on app that is required to read the recover to deleted data. I search a lot but could not find a single solution.
Then I got to know that there is a director "com.sec.android.gallery3d" where all images are saved in small thumbnails.
My question can I read images?
I checked this sample code. But its only reading those directories which is public like DCIM, notification,Down etc
File file=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
public ArrayList<File> listf(File xfile, ArrayList<File> files) {
// get all the files from a directory
File[] fList = xfile.listFiles();
for (File file : fList) {
if (file.isFile()) {
files.add(file);
} else if (file.isDirectory()) {
listf(file, files);
}
}
return files;
}```
Related
I have created an ArrayList to scan .mp3 files from external storage:
final ArrayList<File> myplaylist0 = findSongs0(Environment.getExternalStorageDirectory());
Function findsongs0 is shown below:
public ArrayList<File> findSongs0(File root){
ArrayList<File> al=new ArrayList<File>();
File[] files = root.listFiles();
for(File singleFile : files){
if(singleFile.isDirectory() && !singleFile.isHidden()){
al.addAll(findSongs(singleFile));
}
else
{
if(singleFile.getName().endsWith(".mp3") || singleFile.getName().endsWith(".wav")){
al.add(singleFile);
}
}
}
return al;
}
The above codes give me list of all .mp3 files from my phone storage.
Now, my question is, how to scan mp3 files from the particular folder instead of whole storage. What should I modify in the above code??
Your argument should be like
new File(Environment.getExternalStorageDirectory()+"/"+"<your_mp3_folder>")
i.e
File myfile=new File(Environment.getExternalStorageDirectory()+"/"+"<your_mp3_folder>")
final ArrayList<File> myplaylist0 = findSongs0(myfile);
I have a code to get all the files in a directory into a list. Now I want to sort this list by size from smallest to biggest.
Is there a function or something to do this that's more efficient than writing my own code? If nothing exists, is there an efficient code out there you could point me to?
My code is:
private List<File> getListFiles(File parentDir) {
ArrayList<File> inFiles = new ArrayList<>();
File[] files = parentDir.listFiles();
for (File file : files) {
if (!file.getName().endsWith(".nomedia")) {
inFiles.add(file);
}
}
//Here I should write my sorting code
return inFiles;
}
You can use the SizeFileComparator from commons io
It's really simple to use and provide lot of others useful class and methods.
One way I see to do this would be to create a Hashmap of Files and their respective sizes and another Arraylist of just file sizes.
Hashmap myHashmap = new Hashmap<long, File>;
Insert each file into the hashmap like:
for (File file : inFiles) {
myHashmap.put(file.length(), file);
myFilesizeArrayList.add(file.length());
}
Then you can use the sort method of the ArrayList to sort the file sizes:
myFilesizeArrayList.sort();
Then turn the sorted filesize ArrayList back into a sort File ArrayList using the hashmap:
inFiles.clear();
for (long size : myFilesizeArrayList) {
inFiles.add(myHashmap.get(size));
}
That should give you a sorted list of Files by size.
All that being said, I don't know if that's more efficient than just writing your own simple sorting algorithm. I would tend to just write my own as it would be a relatively trivial task.
This code works.
I used Array.sort to sort the file array before loading it to a List.
Had to look for some example on the comparator, since I had never used one.
private ArrayList<File> getListFiles(File parentDir) {
ArrayList<File> inFiles = new ArrayList<>();
File[] files = parentDir.listFiles();
//Added Arrays.sort
Arrays.sort(files, new Comparator<File>(){
public int compare(File f1, File f2)
{
return Long.valueOf(f1.length()).compareTo(f2.length());
} });
for (File file : files) {
if (!file.getName().endsWith(".nomedia")) {
inFiles.add(file);
}
}
return inFiles;
}
You could also just pass a Comparator to a SortedSet.
Plzzz help me with this. Already late searching for the solution:
I want to list each and every Folder and file which is in the Android Phone's Internal Memory and External Memory.
The below code only gets list of files in one single directory.. But I'm unable to understand how would I list all the folders and files from internal and external memory.
..... List files = getListFiles(new File("YOUR ROOT")); ....
private List getListFiles(File parentDir) {
ArrayList inFiles = new ArrayList();
File[] files = parentDir.listFiles();
for (File file : files) {
if (file.isDirectory()) {
inFiles.addAll(getListFiles(file));
} else {
if(file.getName().endsWith(".csv")){
inFiles.add(file);
}
}
}
return inFiles; }
Any help?
Look at the File object. It gives you methods to get all the files in a directory, tells you whether a file is a directory, and many other things.
You could easily do a recursive tree walk of the directory system. You probably will run into permissions issues, though, unless you have "rooted" your device.
I finally found this somewhere else.. This is how it's done (fully functional code)
public void walk(File root) {
File[] list = root.listFiles();
for (File f : list) {
if (f.isDirectory()) {
Log.d("realcrazy", "Dir: " + f.getAbsoluteFile());
walk(f);
}
else {
Log.d("realcrazy2", "File: " + f.getAbsoluteFile());
}
}
}
i need to list only the files present on a sdcard.
With the following code:
File sdcard=new File(Environment.getExternalStorageDirectory().getAbsolutePath());
if(sdcard.isDirectory()){
String files[]= sdcard.list();
for(int i=0;i<files.length;i++){
File f=new File(files[i]);
if(!f.isDirectory())
Log.d("FILES",files[i]);
}
}
I see also in the log the subdirectories. What am i doing wrong?
Try this:
if(sdcard.isDirectory()){
File[] files = sdcard.listFiles();
for (File f : files){
if(!f.isDirectory())
Log.d("FILES",f.getName());
}
}
The key difference is sdcard.files() vs sdcard.listFiles().
I think the problem is that you need to do this recursively:
File sdcard=Environment.getExternalStorageDirectory();
private void logFiles(File sdcard) {
if(sdcard.isDirectory()){
File[] files= sdcard.listFiles();
for(int i=0;i<files.length;i++){
if(!f.isDirectory())
Log.d("FILES",files[i]);
else
logFiles(files[i]);
}
}
}
I haven't tested this out, but the last else is probably what you were missing and you may find listFiles to be a better choice, here, but, you will need to log the filename, not the File as I left here.
I have an application that needs to read images from a folder created by the application on the sdcard(sdcard/"foldername"/"filename.jpg". I have no idea what the names of the files are because the user specifies the names of the files. I need to read the images from the folder and make something like the default image viewer. Im thinking read them into a grid view first but 1) cant figure out how to dynamically read them from a folder 2) how would I implement the image options like the default viewer? If there was a way to open the default viewer on a certain folder that would help.
any input would be amazing been working on it for a while.
Thanks
Here's how you can get a list of folders off of the memory card:
String state = Environment.getExternalStorageState();
if(state.contentEquals(Environment.MEDIA_MOUNTED) || state.contentEquals(Environment.MEDIA_MOUNTED_READ_ONLY))
{
String homeDir = Environment.getExternalStorageDirectory();
File file = new File(homeDir);
File[] directories = file.listFiles();
}
else
{
Log.v("Error", "External Storage Unaccessible: " + state);
}
This code is from the top of my head, so some syntax may be off a bit, but the general idea should work. You can use something like this to filter down the folders to only folders that contain images:
FileFilter filterForImageFolders = new FileFilter()
{
public boolean accept(File folder)
{
try
{
//Checking only directories, since we are checking for files within
//a directory
if(folder.isDirectory())
{
File[] listOfFiles = folder.listFiles();
if (listOfFiles == null) return false;
//For each file in the directory...
for (File file : listOfFiles)
{
//Check if the extension is one of the supported filetypes
//imageExtensions is a String[] containing image filetypes (e.g. "png")
for (String ext : imageExtensions)
{
if (file.getName().endsWith("." + ext)) return true;
}
}
}
return false;
}
catch (SecurityException e)
{
Log.v("debug", "Access Denied");
return false;
}
}
};
Then, change the first example to:
File[] directories = file.listFiles(filterForImageFolders);
That should return only directories that contain images. Hopefully this helps some!