Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

problem while looping through assets folder :(

Tags:

android

i have a subfolder in the Assets folder called images where i store my images(of course) :) The things is that i want to get the name for the images which i'm getting but the problem is that i'm also getting other unknown names like: "android-logo-mask.png" which i guess are android's default images. Is there a way i can skip this "android default images" to get only the names of my images? My plan is to save this names in a database to use it as reference for showing the images later on an ImageView. Is it a good idea to use the image name to show the images? Here is some code if it needs:

    Context context;

     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            context = this.getApplicationContext();
            ImageHelper i = new ImageHelper();
            i.readImages(context);
     }

    public class ImageHelper {
 public ImageHelper(){}

 public void readImages(Context context){
  AssetManager am = context.getAssets();
  try {
   String[] getImages = am.list("images");
   for(String imgName : getImages){    
    Log.e("IMAGE NAME----->", imgName);    
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

}

Thanks in advance.

like image 385
Ernesto Delgado Avatar asked Nov 24 '10 22:11

Ernesto Delgado


3 Answers

Use some other name for images folder like "MyImages". It should work fine then.

like image 150
Vinayak Avatar answered Oct 21 '22 21:10

Vinayak


"images" should be a path like "/path/assets/images/image.png"

like image 37
Peter Knego Avatar answered Oct 21 '22 21:10

Peter Knego


It is very interesting. Even if you don't have an assets/images directory, when you use:

myImageList = Arrays.asList(getResources().getAssets().list("images"));

you'll see android-logo-mask.png and android-logo-shine.png listed in the results.

That led me to wondering where those actual image files live in the Android code. I don't know the code base at all, but:

https://android.googlesource.com/platform/frameworks/base/+/master/core/res/assets/images/

lists those two in an assets directory under res. (Different from the assets directory where I store my project assets, that's not under res).

Being required to provide a path to an individual file for the AssetManagers list method (which returns an array) just doesn't make any sense. And the method documentation says:

Return a String array of all the assets at the given path.

It makes me wonder if there's an error in the list method, or if there was some other reason it was designed to return particular system assets when an asset list is required.

In a similar fashion, if I use:

myAssetList = Arrays.asList(getResources().getAssets().list(""));

I see "sounds" and "webkit" included, which don't correspond to any existing subdirectory of my assets.

like image 45
gcbound Avatar answered Oct 21 '22 23:10

gcbound