Android Image ID - android

I am building an Android(Kotlin) app which gets data from an API. Part of that data is an Image or more like a reference to an Image inside the Android app. Sadly Android does not accept names as arguments for images, so I have to send IDs(numbers). The Problem is that these IDs are changing when you add or remove a Image from the Android App and in general they do not seem to be "fixed" or unique.
My questions are:
Is there a bullet proof way to reference an Image via Text/Numbers?
Do IDs change from phone to phone? (Is ID "213226123" the same Image on every App where the app is installed?)
I could send an Image identifier made by myself and then work with a switch statement to check which image its supposed to be but this makes easy things complicated and would be incredible bad design.

You can get any resource (including images) id by name with getIdentifier method. Then you can get the resource using the id.

You can get the id of your image(s) dynamically calling the getIdentifier() method of the Resources, like this:
val id = context.resources.getIdentifier("my_image", "drawable", context.packageName)

1: For your request there is a solution but you'll have to do some mapping.
Say your API is returning images as a number: e.g.
{
...
"imageId": 243,
...
}
When setting the image, you map the imageId to the id of your drawable
imageView.setImageResource(findImageForId(myObject.imageId))
fun findImageForId(imageId: Int) = when(imageId){
1 -> return R.drawable.imageA
...
243 -> return R.drawable.imageXYZ
...
}
Id's of your drawables are generated at compile time so they won't change in between phones using the same APK. You can't be sure all your users have the same version (e.g. after an update) so you should never use your generated drawable id's to identify images

Related

Android - Drawables messed up after every single app update

I have an app published on the play store and I am facing an annoying problem both for me and my users.
I am storing a list of Object as a JSON String like this:
val obj = JSONObject()
try {
...
obj.put("image", image)
} catch (e: JSONException) {
Log.d("JSONException: ", e.message)
}
where image: Int stores the value of R.drawable.image
The problem is that every time I release an update for my app this image gets replaced with another drawable (not always the same one) from the /res/ folder.
I suspect it has something to do with proguard's code shrinking, since I have both:
minifyEnabled true
shrinkResources true
How can I fix the problem?
If possible I would avoid not to shrink the images, since I have a fair amount of them and it would increase the download size of the app.
You simply cannot rely on the resource numbers to stay the same across builds. You will need to use another identifier to make persistent and then do a mapping to resource number in your app.
For example you could use an array that contains the resource numbers and you persist the index of a certain drawable.
From Google docs: "When your application is compiled, aapt generates the R class, which contains resource IDs for all the resources in your res/ directory. For each type of resource, there is an R subclass (for example, R.drawable for all drawable resources), and for each resource of that type, there is a static integer (for example, R.drawable.icon). This integer is the resource ID that you can use to retrieve your resource." so when you build the APK the ResourceID could change without any expectation...
You can use its Filename and get the ID in runtime using "getResources().getIdentifier(...)"

Is getResources().getIdentifier() method efficient enough to use it in a ListView?

Some information about the app:
The app contains a lots of images in drawable folder.
The size of every image is between 1kb and 3kb.
And I need to draw them into a ListView.
What I need to get:
I need store the identifier of the image (R.drawable.my_image) into database with some aditional user information.
The problem:
When I update the app (adding new image resources), the identifier may change. So, in database I will have an outdated identifier, which is linking to a non-existing resource.
I've thought to store the resource name in database instead of the resource identifier and them I will retrieve it by getResources().getIdentifier(String, String, String) method. But I don't know if this method is efficient enough to use it in a ListView.
The documentation says Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.
Do you know any other solution?
Finally Im using getResources().getIdentifier() method to load images into list (with recyclerview). My list has about 20 images. The average size is also less than 3kb. I've tried the app in older devices (wich behavior can be affected by his CPU) and it worked fine. I know this method is not recomended, but can be a solution.
Not efficient to use it in a list view.
Official documentation of Resources.getIdentifier method:
Return a resource identifier for the given resource name. A fully qualified resource name is of the form "package:type/entry". The first two components (package and type) are optional if defType and defPackage, respectively, are specified here.
NOTE in documentation
Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.

Getting a usable variable name from within another variable

I'm developing a little* android app and have 9 buttons and 9 images, each named button_# and img#.png
My question is simple, is their a way that I can take an int whose value is between 1 and 9 and reference those objects without having 9 different ifs or a switch. I mean something like being able to do this
myint=4;
button.setImageResource(R.drawable.img.myint);
to set button's image to img4. Of course this doesn't work; I'm looking for some way of concatenating myint onto 'img' (so a string, I guess) and having that be usable to follow R.drawable.
*Just a Univ project, not terribly important but just using a bunch of switches doesn't look good, and moreover I personally doesn't like it.
Edit: Alternately, is there a way to make the images (referencable as in) an array? That's the effect I'm trying to get here anyway because I don't know of a way to use an array here, which would be easier if possible.
you can use getIdentifier to get the id of drawable in this way:
int drawableId = getResources().getIdentifier("resourceName", "drawable", getPackageName());

Prevent Android from generating new ID on resources

Does anyone know if it is possible to make Android not generate new ID's for resources that is added to raw, drawable and other resource-folders?
The problem is that if I for example add an image with the name 3.jpg, and later on adds another image called 2.jpg the ID for image 3.jpg gets changed.
I need to keep the ID's static as the ID's are defined in XML files and used in the app to get the resources based on the ID.
I may have done something wrong when I tested it so it may be that the ID does not get changed, but as far as I can remember ID got changed when adding new resources.
Thanks for any help.
As far as i know, R.java is autogenerated and all resource ID's are internally allocated by eclipse. Technically your code should not depend on the ID's generated. So even if ID's keep changing, it is absolutely fine. If you are depending on the generated ID's in your code, you will keep running into problems.

Android: Loading multiple images

I have recently started developing a game in android, never used it before and was wondering if there is a simple way of getting a set of images loaded into the application. As my current implementation involves basically
Creating an int[] array,
Storing the values of each drawable into this array, (now this has to be hand coded, so if I add any more images it has to be added programmitically)
Then itterating through each item in the array and calling BitmapFactory to get the resource.
(Unfortunately I don't have the code with me as it is at home and I am at work, but that is the jist)
So 2 questions, is there a way of getting the drawables without having to put in each item manually to the int[] - ie looking for perhaps a file name prefix and then only loading the resource with the prefix?
Which leads me to my second question because I more than just these images in my drawable resource directory, is there a way to add extra organisation (folders) to manage the files better. As currently I have loads of images within the drawable file and how would I reference these sub folders/images.
You cannot have sub folders within the resources structure. Android depends on the folder layout to determine which resource to use in what condition (localization, different screen resolutions, etc).
I'm not sure why exactly you are trying to load up a whole bunch of images, but there are a couple of (slower) methods that allow you to look up a resource by string name. If you used a naming convention for your images you could look them up that way via [Resources.getIdentifier()][1]. However, in a game performance likely matters, so you are probably better off with a more manual approach using the int IDs directly since it is much more efficient.
[1]: http://developer.android.com/reference/android/content/res/Resources.html#getIdentifier(java.lang.String, java.lang.String, java.lang.String)
I am uploading a load of images as they will be shown to user for different items. Its not a system where responsiveness is critical so its okay in terms of what I want. Though...
public int getIdentifier (String name, String defType, String defPackage)
Since: API Level 1
Return a resource identifier for the given resource name. A fully qualified resource name is of the form "package:type/entry". The first two components (package and type) are optional if defType and defPackage, respectively, are specified here.
Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.
They suggest using the resource id but if I want to add a file later on then I have to re-compile the app to include the extra file, this is where it bugs me, as the pic gets associated to an item that I have in a string array. So I can add items to the array but not the images without a change of the code.
Surely there is a function to fix this?

Categories

Resources