getting assets not get the files of assets - android

I have a project in Android Studio with a lot of libraries. The main project has an assets folder (which contains two folders) and the libraries don't have any assets folders.
I have this code:
public static final Pattern LENGUAJES = Pattern.compile("html-(.+)");
public static List<String> getLenguajesHelp(Context oContext) {
ArrayList<String> oLens = new ArrayList<String>();
AssetManager assetManager = oContext.getAssets();
try {
String[] oFiles = assetManager.list("");
for (int i = 0; i < oFiles.length; i++) {
Log.v("probandoElMatcher", oFiles[i]);
Matcher oMat = LENGUAJES.matcher(oFiles[i]);
if (oMat.find()) {
oLens.add(oMat.group(1));
}
}
} catch (IOException oEx) {
oEx.printStackTrace();
}
return oLens;
}
The Context is from the main project (in the assets folder I have a folder named html-es, the idea is that the method matches with that folder) but the method getAssets.list() returns a list of files that I don't have in the assets folder, the most of them are .pak files. What would be the problem?

The problem was that i has a lot of flavors, when there are flavors the assets folder must be inside of the main folder (or the specific flavor folder), but i don't know why the get assets returns the .pak files (know returns the .pak files and my files)

Related

Android studio fileFilter with assetManager

I have the following functionality to a desktop application with java and I want to do the same thing for a mobile application.
File f = new File(pathToFiles);
File[] files = f.listFiles(new FileFilter() {
#Override
public boolean accept(File pathname) {
return (pathname.getName().endsWith(".img") ? true : false);
}
});
Now in my android class I access the same files stored in a folder inside assets using the AssetManager with the following way:
AssetManager assetManager = context.getAssets();
String[] files = assetManager.list("folderInsideAssets");
List<String> it = new LinkedList<String>(Arrays.asList(files));
The problem here is that I get them as string in a list instead of a File class like in plain java.
Is a way to convert it to a File[] class and proceed with the same way or I should handle this differently?
Thank you in advance!
Stackoverflow Question on loading pictures from asset
Yes, create another folder in assets directory. use getAssets().list(<folder_name>) for getting all file names from assets:
String[] images =getAssets().list("images");
ArrayList<String> listImages = new ArrayList<String>(Arrays.asList(images));
Now to set image in imageview you first need to get bitmap using image name from assets :
InputStream inputstream=mContext.getAssets().open("images/"
+listImages.get(position));
Drawable drawable = Drawable.createFromStream(inputstream, null);
imageView.setImageDrawable(drawable);

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.

Read folder name and folder's file name from assets folder

I have this file structure in my android project:
I want to read all the folder names and their file names contained in assets folder. I have tried so many things but I am not able to achieve my goal. Can somebody guide me to correct direction?
Thanks in advance.
You can read the filenames of a particular folder from assets as follows :
private List<String> getFileNames(Context context, String folderName) throws IOException {
AssetManager assetManager = context.getAssets();
String[] files = assetManager.list(folderName);
return Arrays.asList(files);
}
Simple read all the assets using the below function
public myClass(Context myContext) {
AssetManager mngr = myContext.getAssets();
InputStream is = mngr.open("assests.txt");
}
A simple getAssets().list("") will list all folders.
And files if there are. But you havenot.

Android assets folder does not contain what I put in there

I have added a bunch of wave files into my assets directory for loading.
I created assets by right clicking on my res folder in android and going "Add folder -> Assets folder"
It created an assets folder at app\src\main\assets.
I have dragged a bunch of wav files into that assets directory.
But when I go to list all the files in my assets directory, it just lists a single directory called "images".
Observe the following code. There is a message write that only write out "images".
AssetManager assets = LocalApp.getAppContext().getAssets();
String[] fileList = assets.list("");
// we assume all the files in the assets are the wav files that we want to load,
// so attempt to load everythign in assets as a wav
for (int i = 0; i < fileList.length; i++) {
Log.e(TAG, "readSamples: file name = "+fileList[i]);
WavFile wave = new WavFile(assets.open(fileList[i]));
samples.add(wave);
}
Maybe you can try this:-
String dirFrom = "putYourPathToAssestsFolderHere"
//if you are in an activity
Resources res = getResources();
AssetManager assets = res.getAssets();
String fileList[] = assets.list(dirFrom);
if (fileList != null)
{
for ( int i = 0;i<fileList.length;i++)
{
Log.e(TAG, "readSamples: file name = "+fileList[i]);
}
}

Iterate through assets subfolders

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 .

Categories

Resources