How to get count of images stored in SD card - android

I need to calculate the number of images stored in SD card and so i would like to name the images unique like Sample0,Sample1,Sample2,Sample3,etc.
Is it Possible?

It gives the number of images present in the images folder of your SD card:
File dir = new File(Environment.getExternalStorageDirectory()
+ "/images");
File[] files = dir.listFiles();
int numberOfImages=files.length;

I now it's been a long time, but, the method above would give you the number of any kind of files and directories within images folder, not only the images number, and doesn't look for images in subfolders inside the images directory.
Let's say we have this structure:
Images/
- img1.jpg
- img2.jpg
- data.dat
- whatever.pdf
- Folder/
- img3.png
The method above will give you 5 and you should obtain 3 isn't it?
I've been working on something similar and I'm pretty sure there is a more efficient way, but with this method I get the number of all images within the SDCard, or any given directory and its subfolders...
Here we go:
public int countImgs(File file, int number) {
File[] dirs = file.listFiles();
String name = "";
if (dirs != null) { // Sanity check
for (File dir : dirs) {
if (dir.isFile()) { // Check file or directory
name = dir.getName().toLowerCase();
// Add or delete extensions as needed
if (name.endsWith(".png") || name.endsWith(".jpg")
|| name.endsWith(".jpeg")) {
number++;
}
} else number = countImgs(dir, number);
}
}
return number;
}
Where the param file is the root directory and the number would be 0 at start. So to use it:
int imgNumber = countImgs(Environment.getExternalStorageDirectory(), 0);

it will be an error when files = null when get the count of images stored in SD card
File dir = new File(Environment.getExternalStorageDirectory() + "/images");
File[] files = dir.listFiles();
String strnumberOfImages=files.length;
int intnumberOfImages= 0;
try{
intnumberOfImages = Integer.parseInt( strnumberOfImages);
}catch ( Exception e ){
intnumberOfImages = 0;
}
if just check empty or not
File dir = new File(Environment.getExternalStorageDirectory()+ "/images");
File[] files = dir.listFiles();
if(files != null){
// there are picz
}else{
// sd card empty
}

Related

How to find directories with particular name

Any app can create folder in external storage.
This folder can be placed at SD card or phone inner memory.
If i use this code, i can to get list of all my external storages.
File file2[] = getExternalFilesDirs(null);
it return:
/storage/emulated/0/Android/data/com.snailp4el.android.tatoeba/files
/storage/8639-0FFD/Android/data/com.snailp4el.android.tatoeba/files
but if i try to get list of it path:
File f = new File("/storage/8639-0FFD/Android/data/com.snailp4el.android.tatoeba/files");
File[] files = f.listFiles();
I get nothing.
in this case:
File f = new File("/storage/");
File[] files = f.listFiles();
i get:
/storage/8639-0FFD
/storage/emulated
/storage/self
That is OK. i finally get all storages in my phone.
Question is:
How to scan subfolders to find particular folders by name?
And is my approach right. I mean to scan "/storage"?
And if Yes will it work at all version?
and finally what is "/storage/self"?
I solved it this way.
May by it is not the best solution but it works fine for me.
public ArrayList getExternalFolderPath(String folderName){
ArrayList<String> arrayList = new ArrayList<>();
//get all storages in phone
File externalStorages[] = getExternalFilesDirs(null);
for (File es: externalStorages){
String exF = es.toString();
//cut out what don't need
exF = exF.split("/Android/data")[0];
File file = new File(exF);
File[] files = file.listFiles();
for (File f: files){
Log.i(TAG, "getAnkiExternalFolder()" + f.toString());
//add in array the path
if(f.toString().contains(folderName)){
arrayList.add(f.toString());
}
}
}
return arrayList;
}
You can get two directories(from both storage). The directory you need the last changed one.
file.lastModified()

How to open a directory which contains subdirectory in android?

can I open android directory mounted on sdcard should contain sub directories.
for example in ashish directory i had talasu and ash subdirectories.
The code which I had return :-
String root = Environment.getExternalStorageDirectory().toString();
File file = new File(root + "/ashish");
yes you can access those directories, heres a little code to help you out
File[] c = f.listFiles();//where f is your root directory(ashish)
if (c != null) {
for (int i = 0; i < c.length; i++) {
if (c[i].isDirectory()) {
//do whatever you want to do with subdirectories
}

Android: How to list all the files in a directory in to an array?

I have a variable "directorypath". It has the path to a directory in sd card.
Ex: "sdcard/images/scenes"
Now, I want to list all the files in the "directorypath" and want an Array to store all the file names in it.
Ex: Array[0]= Filename1, Array[1]= Filename2 Etc...
I tried something like Array[] myArray = directorypath.listfile(); but didn't work..!
Can anyone give me the code or at least help me? Much appreciated.
I think the problem you are facing is related to the external storage directory file path. Don't use whole path as variable if you can access with environment.
String path = Environment.getExternalStorageDirectory().toString()+"/images/scenes";
Also, you can use the API listfiles() with file object and it will work. For eg ::
File f = new File(path);
File file[] = f.listFiles();
Try the following code which may help you
List<File> getListFiles(File parentDir) {
ArrayList<File> inFiles = new ArrayList<File>();
File[] files = parentDir.listFiles();
for (File file : files) {
if (file.isDirectory()) {
inFiles.addAll(getListFiles(file));
} else {
inFiles.add(file);
}
}
return inFiles;
}
This will return you list of files under the directory you specified.
From the File object in list.. you will get all information about files you required.
Try the following code:
File externalStorageDirectory = Environment.getExternalStorageDirectory();
File folder = new File(externalStorageDirectory.getAbsolutePath() + "/FilePath");
File file[] = folder.listFiles();
if (file.length != 0) {
for (int i = 0; i < file.length; i++) {
//here populate your list
}
} else {
//no file available
}
According to your error message "Cannot resolve listFiles()", you are trying to call a method on a String object, when you actually want to be calling that on a File object.
Create a new File object,
String parentDirectory = "path/to/files";
File dirFileObj = new File(parentDirectory);
File[] files = dirFileObj.listFiles();
I was indeed calling a function for improper data type. i.e., listfiles() won't work on String type variable..!!
SO, I created a File type variable and a File[] array to pass the list of files..!
This solved the issue.
its very simple Just two lines of code
File mListofFiles = new File(Environment.getExternalStorageDirectory.getAbsolutePath()+"/",childpath:Foldername);
File f = new File(mListofFiles.toString());
File file[] = f.listFiles();

Android How can I read filenames from a specific folder?

I would like to scan a specific folders for all filenames inside to save them to an array.
But how can I access the sd Card to do this?
Help would be great (io / enviroment???)
Environment.getExternalStorageDirectory() will link you to the SD card if one is inserted in the phone. Otherwise it just links you to your phone storage. Here's a link to more information on Environment http://developer.android.com/reference/android/os/Environment.html.
File file = new File(Environment.getExternalStorageDirectory()+"/folder/");
File[] files = file.listFiles();
String[] fileNames = new String[files.length];
for(int i = 0; i < files.length; i++)
fileNames[i] = files[i].getName();
You can use the following code:
String filesDirectory = Environment.getExternalStorageDirectory().getAbsolutePath()+"/YOUR_SPECIFIC_FOLDER/"
File fileList = new FilefilesDirectory
if (fileList != null)
{
File[] filenames = fileList.listFiles();
//Loop through each file and get the file name
String fileNamesArray[] = new String[filenames.length];
int i=0;
for (File tempFile : filenames)
{
fileNamesArray[i] = tempFile.getName();
i++;
}
}

How to choose only .mp3 extension files from whole sdcard

In my application, when a user want to upload Audio, that user should only be able to choose .mp3 extension file. If the user choose any other file like .ppt, .pdf, it wont be allowed.
I have got this approved code for File explore from SD Card: Choose file dialog.
But I want only .mp3 extension files or list of .mp3 extension files.
So what should I do?
maybe you can check for files like this
File f = new File("/sdcard/");
for (File file : f.listFiles())
{
if(file.getName().endsWith(".mp3"))
{
// do something with them, add them to list or whatever
}
}
Check file pattern and then compare, if it is mp3 then add into arraylist, check below function, which will return list of mp3.
 Vector<String> mStrings =new Vector<String>();
int mTotalimage=0;
public void fileExtension(File dir) {
String mp3Pattern = ".mp3";
File listFile[] = dir.listFiles();
if (listFile != null) {
for (int i = 0; i < listFile.length; i++) {
String mCheck=listFile[i].getAbsoluteFile().toString();
mCheck=mCheck.substring(mCheck.lastIndexOf("/")+1);
System.out.println("listFile[i].getAbsoluteFile().toString()"+listFile[i].getAbsoluteFile().toString());
System.out.println("mCheck..."+mCheck);
if (listFile[i].isDirectory() && !(mCheck.startsWith(".")) ) {
fileExtension(listFile[i]);
} else {
//fetch jpg from folders.
if (listFile[i].getName().endsWith(mp3Pattern)){
mStrings.add(listFile[i].getAbsolutePath().toString());
mTotalimage++;
//fetch png image from folders.
}
}
}
}
System.out.println("size on return......."+mStrings.size());
}

Categories

Resources