Android assets folder does not contain what I put in there - android

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

Related

getting assets not get the files of assets

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)

How to efficiently create subfolders using Storage Access Framework?

I'm currently using the below code to create subfolder in MicroSD on Lollipop using SAF
String[] folders = fullFolderName.replaceFirst(UriFolder + "/", "").split("/");
//fullFolderName is a String which represents full path folder to be created
//Here fullFolderName = /storage/MicroSD/MyPictures/Wallpapers
///storage/MicroSD/MyPictures/ already exists
//Wallpapers is the folder to be created
//UriFolder is String and contains /storage/MicroSD
//folders[] will have folders[0]="MyPictures" folders[1]="Wallpapers"
DocumentFile Directory = DocumentFile.fromTreeUri(context, Uri.parse(treeUri));
//treeUri is the uri pointing to /storage/MicroSD
//treeUri is a Uri converted to String and Stored so it needs to parsed back to Uri
DocumentFile tempDirectory = Directory;
//below loop will iterate and find the MyPictures or the parent
//directory under which new folder needs to be created
for(int i=0; i < folders.length-1; i++)
{
for(DocumentFile dir : Directory.listFiles())
{
if(dir.getName() != null && dir.isDirectory())
{
if (dir.getName().equals(folders[i]))
{
tempDirectory = dir;
break;
}
}
}
Directory = tempDirectory;
}
Directory.createDirectory(folders[folders.length-1]);
The above code works fine and creates subdirectory but it takes ~5 Secs to create the folder. I'm new to SAF so is this the only way to locate subdirectories or is there any other efficient way to create subdirectories?
On internal storage I will use
new File(fullFolderName).mkdir();
Which will create folder in a fraction of second.
Here is a bit efficient way to create
public static boolean createFolderUsingUri(String fullFolderName,String treeUri,
String UriFolder,Context ctx)
{
String[] folders = fullFolderName.replaceFirst(UriFolder + "/", "").split("/");
//fullFolderName is a String which represents full path folder to be created
//Example: fullFolderName = /storage/MicroSD/MyPictures/Wallpapers
//The path /storage/MicroSD/MyPictures/ already exists
//Wallpapers is the folder to be created
//UriFolder is String and contains string like /storage/MicroSD
//folders[] will have folders[0]="MyPictures" folders[1]="Wallpapers"
//treeUri string representation of Uri /storage/MicroSD
//Ex: treeUri content://uritotheMicroSdorSomepath.A33%0A
DocumentFile Directory = DocumentFile.fromTreeUri(ctx, Uri.parse(treeUri));
for(int i=0; i < folders.length-1; i++)
{
Directory=Directory.findFile(folders[i]);
}
Directory.createDirectory(folders[folders.length-1]);
return true;
}
The method described in question took ~5 Secs, whereas this method takes ~ 3 Secs. On CM file manage the folder creation on same path took ~4 Secs so this is comparatively faster method. Yet searching more faster way which will take < 1 Sec

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
}

How to get the name of all image files in a folder?

I want to get the names of all image file in a directory (lets say pictures) into an array of strings. I'm still new so I don't know how to approach this. I just need a way to retrieve the filenames with the .png extension from the pictures folder on the sd card so I can store it in an array.
this is how to list files under any path.
private void listAllFiles(String pathName){
File file = new File(pathName);
File[] files = file.listFiles();
if(files != null){
for(File f : files){ // loop and print all file
String fileName = f.getName(); // this is file name
}
}
}
You can do this using the java.io.File
If you just want the names you can use.
File dir = new File("<YourPath>");
ArrayList<String> names = new ArrayList<String>(Arrays.asList(dir.list()));
If you want the whole file object use.
File dir = new File("<YourPath>");
ArrayList<File> files = new ArrayList<File>(Arrays.asList(dir.listFiles()));
More Information
java.io.File

How to get count of images stored in SD card

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
}

Categories

Resources