0

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!

konkouts
  • 51
  • 1
  • 6
  • No. The File class if for files on the file system. Not for files in assets resource. – blackapps Mar 30 '22 at 17:44
  • @blackapps So I need to store my files somewhere else? or follow a different approach? – konkouts Mar 30 '22 at 17:47
  • If you only want a list with .img files then you could only add them to the list if they end on .img. The same as a file filter would do. There is not much in it. Further we have no idea for what you would use that list. – blackapps Mar 30 '22 at 17:49

1 Answers1

0

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);
Narendra_Nath
  • 4,578
  • 3
  • 13
  • 31