Iterate through assets subfolders - android

I need some help. I need to iterate through assets folder and find all files and subfolders and do something with them.
For example . I have created folder "myicons" in assets folder. But this folder(myicons) include other folders which include icons. It looks like
/myicons/buttons/playbt.ico ,/myicons/buttons/stopbt.ico ,
/myicons/shapes/square.ico
and so on.
So I need to iterate through every subfolder but it doesn't work. AssetsManager.list(path) returns only files .
Please suggest how to solve the problem. For example if getFromAssets(getAsset(),"myicons");
void getFromAssets (AssetManager mgr, String path) {
try {
String list[] = mgr.list(path);
if (list != null)
for (int i=0; i<list.length; ++i)
{
File file = new File(list[i]);
if (file.isDirectory()) {
Log.d("Assets:",list[i]+ "is Directory");
}
else {
Log.d("Assets:",list[i]+ "is File");
}
}
} catch (IOException e) {
Log.v("List error:", "can't list" + path);
}
}
The problem was that if I have my_icons folder and in this folder I have buttons folder it didn't recognize buttons folder.
list method doesn't recognize EMPTY FOLDERS. Everything works if folder is not empty .
Thx everyone for help.
I will post code a bit later .

Related

How to search external storage for a specific folder name in Android

I searched for this specific instant all over stack overflow and internet. But cannot get the result. Hence i am forced to ask this community.
I want to list all the folders and sub folders in external storage, and find a specific folder by its name i.e, "dtbackup". Yes i know i can list the folders with this code.
public void listallfolders() {
String path = Environment.getExternalStorageDirectory().toString();
File f = new File(path);
File[] files = f.listFiles();
for (File inFile : files) {
if (inFile.isDirectory()) {
// is directory
try {
Log.d("search_opr", inFile.getAbsolutePath().toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
This returns the folder path but only top level folders, sub folders and deep folders are not listed. i have tried numerous method but i have no idea how to do this.

how to browse only text files from my application

i have built an application,where i am supposed to browse only for txt files in the device and read it.but i am not getting a valid solution to it.Like for example if we take whatsapp,there you can browse different files under different headings like under videos you will only browse videos and under gallery you will only browse images.but whats in case for text files only?is there any way?Please suggest.
try this.
//for example your folder present in sdcard/cts.
File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/cts");
for (File wavfile : f.listFiles(new FileExtensionFilter()))
{
String str = wavfile.getName().toString();
}
//create a class and the filter based on your need file extension
class FileExtensionFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.endsWith(".txt") || name.endsWith(".TXT") );
}
}
thank you.
I would put your directories your searching into arrays... Loop the arrays and look for the file extensions after the " . " if its not a text extension remove it. Then display the new array to user like the photo gallery works or however else you would want to represent them.
Note- If you need access to these later you may want to be saving the path if the user say...needs to click and open them...
you may be able to use something to this effect for searching the SD Card
File file[] = Environment.getExternalStorageDirectory().listFiles();
file[i].getAbsolutePath();
Then search it for the extensions you want...
This will give you a file manager as you like. Just needs a bit of work around. But definitely helps
//fill method is to get the dir
private void fill(File f)
{
File[] dirs = f.listFiles();
this.setTitle("Current Dir: " + f.getName());
List<Option> dir = new ArrayList<Option>();
List<Option> fls = new ArrayList<Option>();
try
{
for (File ff : dirs)
{
if (ff.isDirectory())
{
dir.add(new Option(ff.getName(), "Folder", ff.getAbsolutePath()));
}
else
{
String filetype[] = objOption.getName().split("\\.");
if (filetype[(filetype.length) - 1].equals("txt")) {
fls.add(new Option(ff.getName(), "File Size: "+ ff.length()+" bytes", ff.getAbsolutePath()));
}
}
}
}
catch (Exception e)
{}
Collections.sort(dir);
Collections.sort(fls);
dir.addAll(fls);
if (!f.getName().equalsIgnoreCase("sdcard"))
{
dir.add(0, new Option("..", "Parent Directory", f.getParent()));
}
adapter = new FileArrayAdapter(FileChooser.this, R.layout.dialog_file_view,dir);
lvFileList.setAdapter(adapter);
}

List each and every Folder and file

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());
}
}
}

How create a folder on SdCard and how to see the same folder on emulator?

I have created two string one with the name SDCARD which means "sdcard" and RECYCLE_BIN_ROOT which stands for a folder with name of Recycle in Sdcard. But i am getting problem with this.
I have created code for copying files from sdcard ( for eg images in DICM ) to recycle bin, but result is zero.I have created recycle folder using file explorer option in eclipse also.
Please tell me some solution.
public class C
{
public static String SDCARD = "/sdcard";
public static String RECYCLE_BIN_ROOT = SDCARD+"/.Recycle";
}
java.io.File f = new java.io.File(RECYCLE_BIN_ROOT);
if (!f.exists()) {
if (f.mkDirs()) {
Log.v("", "File created successfully...");
} else {
Log.v("", "File cannot created successfully...");
// You can throw exception
}
}

Images from folder on sd card

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!

Categories

Resources