0

I have a lot of SVG images, currently transformed into XML code, stored into the drawable/ directory. I am using the following code to retrieve them all and randomly display one of them:

Field[] ID_Fields = R.drawable.class.getFields();
int[] resArray = new int[ID_Fields.length];
for (int i = 0; i < ID_Fields.length; i++)
{
  try
  {
    resArray[i] = ID_Fields[i].getInt(null);
  }
  catch (IllegalAccessException e)
  {
    e.printStackTrace();
  }
}

Random r = new Random();
int i1 = r.nextInt(ID_Fields.length - 1);
Log.i("TAG", "Selected index: " + i1);

Drawable drawable = ContextCompat.getDrawable(context, resArray[i1]);
imageView.setImageDrawable(drawable);

This works fine - even if all drawables are loaded, so a mechanism to separate my custom images must be implemented - and the chosen image is correctly displayed into the imageView.

I would like to change my structure and put my XMLs into the assets/flags/ folder. However, when I use the following code, the returned drawable is always null:

AssetManager am = getApplicationContext().getAssets();
String[] files = new String[0];
try
{
  files = am.list("flags");
}
catch (IOException e)
{
  e.printStackTrace();
}
ArrayList<Drawable> drawables = new ArrayList<>();
for (String file : files)
{
  Drawable d = null;

  try
  {
    d = Drawable.createFromStream(am.open("flags/" + file), null);
  }
  catch (IOException e)
  {
    Log.i("EX2", "Second exception: " + file + " not found");
    e.printStackTrace();
  }

  drawables.add(d);
  if (d == null)
    Log.i("FLAG", "Null drawable");
}


Random r = new Random();
int i1 = r.nextInt(drawables.size() - 1);
Log.i("TAG", "Selected index: " + i1);

Drawable drawable = drawables.get(i1);
imageView.setImageDrawable(drawable);

Why does this happen?

Michael
  • 876
  • 9
  • 29
  • It seems to me, the list method returns an empty array, because if the problem would be elsewhere, you should saw an exception in the log. Try searching for this problem. Anyway I don't think it is good for performance opening all files just to select one of them. It would be better to select a random path and load only that one file. – nvi9 Mar 30 '17 at 16:16
  • No, the `list()` method does not return an empty array, I checked it thoroughly. `null` is returned when the `Drawable` is created via the function `createFromStream()`. Anyway, how would you suggest to select a random path? I do not know the exact name of the files, I only know that they all start with the prefix `ic_flag_of_`. – Michael Mar 30 '17 at 16:42
  • Is null returned for all files, or just some of them? Do any of your file names contain upper-case letters? I believe asset file names must be lower case. – Paul LeBeau Mar 31 '17 at 05:02
  • It is returned for each file. The XML files __are__ found, but the `Drawable`s are never created, since the function `createFromStream()` returns `null` for some strange reason. – Michael Mar 31 '17 at 13:50
  • @Michael the `files` string array contains the possible paths, select one of them randomly and then load only that one. Returning to the main problem, based on the source of `Drawable` class, `createFromStream` method simply returns `null` if the argument stream is `null`, there is no exception. But `open` should throw `IOException`if the provided path doesn't exist, so it becomes more strange, how it can return `null`... – nvi9 Mar 31 '17 at 18:57

0 Answers0