I want to list all files in folder which is in assets. But new File("/android_asset/instagram").exists() always returns false.
File instagram = new File("/android_asset/instagram")
for (File lookUpFile : instagram.listFiles()) {
String filterName = FileUtils.removeExtension(lookUpFile.getName());
filterName = Strings.capitalizeAndCopy(filterName);
GPUImageLookupFilter lookupFilter = new GPUImageLookupFilter();
lookupFilter.setBitmap(BitmapFactory.decodeFile(lookUpFile.getAbsolutePath()));
filters.addFilter(filterName, lookupFilter);
}
Try to access the file using asset manager. Try to get the list of files and make sure its working.
AssetManager assetManager = getAssets();
String[] files = assetManager.list("");
Related
I am busy developing an Android application for university and would like to load all images saved into an internal storage folder into an array list. I have managed to do it using assets but I need to be able to do it using the image files on the internal storage folder that I created
Code used to create the folder
File root_text = Environment.getExternalStorageDirectory();
try {
File folder = new File(Environment.getExternalStorageDirectory() + "/InkousticImages");
boolean success = true;
if (!folder.exists()) {
success = folder.mkdir();
}
}
Current code used to load asset files into an array list and open them into an inputStream(this is what I want to do but with the files from the internal storage instead)
String[] imageList = getAssets().list("images");
List <String> myList = new ArrayList<>();
for (String filename: imageList) {
if (filename.toLowerCase().endsWith(".jpg") || filename.toLowerCase().endsWith("jpeg")) {
myList.add(filename);
}
}
AssetManager assetManager = getAssets()
InputStream istr = assetManager.open("images/" + myList.toArray()[index]);
Any help would be greatly appreciated.
Here, I wrote the code for you, This loops over all the files in the directory and checks if the extension is a valid image and adds the name to the List
List<String> fileNames = new ArrayList<>();
File folder = new File(Environment.getExternalStorageDirectory(), "InkousticImages");
if (!folder.exists()) folder.mkdir();
for (File file : folder.listFiles()) {
String filename = file.getName().toLowerCase();
if (filename.endsWith(".jpg") || filename.endsWith("jpeg")) {
fileNames.add(filename);
}
}
In the assets folder I have another folder called songs with .txt files. I've been trying to put all .txt files in a File[ ] but I get a NullPointer on folder.listFiles().
Here's the code :
File folder = new File("assets/songs");
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
File file = listOfFiles[i];
if (file.isFile() && file.getName().endsWith(".txt")) {
String content = FileUtils.readFileToString(file);
this.list.add(content);
System.out.println(content);
}
}
return this.list;
Assets are not files on the device. They are files on the development machine. They are entries inside the APK file on the device.
Use AssetManager to work with assets, including its list() method.
As sir #CommonsWare mentioned, you can use AssetManager like this:
AssetManager assetManager = getAssets();
String[] files = assetManager.list("");
Note that this file is String array. So don't forget to initialize new file for each element of the array before iterating over it.
I'm using the Java CIFS Client Library but facing the problem and problem is copyTo function is not working.
I have one folder which contains files. I want to read these files to other network path.
String path1 = "//MACHINE-NAME/SHARE-FOLDER"
NtlmPasswordAuthentication auth = new
NtlmPasswordAuthentication(DOMAIN;USERNAME:PASSWORD)
SmbFile readFolder = new SmbFile("smb://MACHINE-NAME/SHARE-FOLDER/",auth)
This is working fine.
Then i have another network path and define like this and ShareFolder2 is have the read/write access to 'everyone' user.
String path2 = "//MACHINE-NAME/SHARE-FOLDER2"
NtlmPasswordAuthentication auth = new
NtlmPasswordAuthentication(DOMAIN;USERNAME:PASSWORD)
SmbFile destinationFolder = new SmbFile("smb://MACHINE-NAME/SHARE-FOLDER2/",auth)
ArrayList<SmbFile> readFiles = readFolder?.listFiles()
for(file in readFiles ){
file.copyTo(destinationFolder)
}
If you wanted to copy a file from one shared location to another shared location. You can this like this
ArrayList<SmbFile> readFiles = readFolder?.listFiles()
for(file in readFiles ){
String name = file.properties.getKey("name")
destinationFolder = new SmbFile(foldersInfo?.destinationFolder+"/"+name,auth)
destinationFolder.createNewFile()
file.copyTo(destinationFolder)
}
The file which you want to copy that file must be in the destination folder.
First we will create a file with same name in the destination folder then copy to that folder
I created a directory programmatically and i inserted photos :
File dirGallery = context.getDir("Gallery", Context.MODE_PRIVATE);
File fileWithinMyDir = new File(dirGallery, photo);
...
This is working !
But now i want to create a directory in my existing directory "Gallery" and insert other photos.
I tried :
File dirGallery = context.getDir("Gallery/Gallery2", Context.MODE_PRIVATE);
But i get "File ... contains a path separator".
I also tried :
File dirGallery = context.getDir("Gallery", Context.MODE_PRIVATE);
dirGallery.mkdir();
File dirGallery2 = new File(dirGallery,"Gallery2");
dirGallery2.mkdir();
File fileWithinMyDir = new File(dirGallery2, nomPhoto);
And when i get my file :
File dirGallery = context.getDir("Gallery", Context.MODE_PRIVATE);
File dirGallery2 = new File(dirGallery,"Gallery2");
File[] listImages = dirGallery2.listFiles(filter);
But listImages is empty. Where did i fail ?
TY
Instead of writing this:
File dirRecipe = context.getDir("Gallery/Gallery2", Context.MODE_PRIVATE);
try this:
File dirRecipe = context.getDir("Gallery"+File.separator+"Gallery2", Context.MODE_PRIVATE);
this should create the folder inside of folder as you want.
You can not pass a directory structure (e.g. a/b/c) to GetDir(), the following will work however:
File dir = getFilesDir();
File dir2 = new File(dir, "test1/test2");
dir2.mkdirs();
this will create the directory structure
/data/data/com.somename.someclass/files/test1/test2
I have some files in the assets folder of my project, and I want to list them, so I put this in my code:
File dir = new File("com.packagename/assets/fonts");
File[] fileList = dir.listFiles();
Which path should I put to make it work?
I want it so users could install new fonts (I don`t know how to do this yet) so I need to list all the fonts in the folder, including post-installed fonts. If there is any other solutions, please share.
Assets and resources are accessible using file:///android_asset and file:///android_res.
But in this case you will want to do something like this :
Resources res = getResources()
AssetManager am = res.getAssets();
String fileList[] = am.list(dirFrom);
if (fileList != null) {
for ( int i = 0;i<fileList.length;i++) {
Log.d("",fileList[i]);
}
}