-1

I have 100 images in my application of different cities and I want to divide these pictures in different groups, lets say in evening, morning, sunny, raining etc…

We know that when we call an image from layout folder by calling R.layout.image_1, android generates integer number for each image For example:

R.layout.image_1 (223344), R.layout.image_2 (556677), R.layout.image_3 (778899),

I can create one table having evening, morning fields and I can assign group of pictures to each of them with integer IDs which are (223344,556677) and I can call evening or morning group and i can display all images related to these group.

My question is: Does Android generate same number every time. Are these numbers are fixed? When ever the application runs.

If its true then upper idea will work for me. If this idea is incorrect then kindly guide me what is the decent approach to handle hundreds of PNGs in application.

abidkhan303
  • 1,761
  • 3
  • 18
  • 31
  • Why use the actual values? Create a map/list/whatever that translates what you want into the actual R.layout.image_x value. And as far as I know the values are guaranteed to change. – dmon Jan 18 '13 at 15:35
  • so it means, we do not have anything to store in database realted to our images..... – abidkhan303 Jan 18 '13 at 16:04
  • 2
    @user1642500 You can store the names of the images and get them via `Resources#getIdentifier()` or you could directly put the images [in the database](http://stackoverflow.com/questions/7331310/how-to-store-image-as-blob-in-sqlite-how-to-retrieve-it). – A--C Jan 18 '13 at 20:51

3 Answers3

2

Those numbers are not fixed. R will be regenerated and can have completely different numbers if you change something. That is why when comparing ids, you compare by the name instead.

Eg instead of

if (i == 223344)

do

if (i == R.layout.image_1)

Since R.layout.image_1 references the integer id, the name won't change (unless you change the layout xml name.

If you want to get a resource id dynamically (by a string representing the name), you should have a look at this method - Resources#getIdentifier().

A--C
  • 36,351
  • 10
  • 106
  • 92
1

First of all we generally put images in the drawable folder.

Does Android generate same number every time?

No.

Are these numbers fixed whenever the application runs?

Yes.

In fact, once your project is built, the ids will remain the same for that same build.
In other words for a certain generated APK file, the ids won't change.

So how can you take advantage of that to group your resources?

You could have a static int array that holds the ids:

public static final int[] IMAGES_MORNING = {R.drawable.morning0, R.drawable.morning1, etc};
public static final int[] IMAGES_EVENING = {R.drawable.evening0, R.drawable.evening1, etc};

Although a more structured method would be to store them in a database on your app's first launch.


Or you could use what A--C suggests: For example to get all the ids of morning images
for(int i = 0; i < numberOfMorningImages ; i++){
    int id = getResources().getIdentifier("morning" + i, "drawable", getPackageName());
    // do something with the id
}
Benito Bertoli
  • 25,285
  • 12
  • 54
  • 61
0

No, there's no guarantee that integers will be the same every time, so the solution you've described won't work. Unfortunately, there's no proper way to group drawables inside the res/drawable folder. As a workaround, you can store them inside the assets folder, where you can group them as you like. However, Android won't be able to handle different resolutions this way. The choice is up to you. Hope this helps.

Egor
  • 39,695
  • 10
  • 113
  • 130
  • 1
    so it means, we do not have anything to store in database realted to our images. and we have to handle all images in programming side.... – abidkhan303 Jan 18 '13 at 16:06