Android studio fileFilter with assetManager

495 views Asked by At

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!

1

There are 1 answers

0
Narendra_Nath On

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