android - images in Drawable folder and getting their id's into java - android

I'm new to development and I'm stuck at a point where I don't know what would be the best possible solution.
I have a list of 26 images saved in the drawable folder. Lets say a to z. When user clicks on the right side layout they should navigate forward and if the user clicks on the left side layout they should navigate backwards.
My issue is that these are images and their IDs as retrieved by android are random int values. I can't use that to compare which letter of alphabet should be displayed next.
How do I go about this problem?
If I name the images a.png, b.png, c.png... How do I retrieve the name later from the ID?
Will I be able to store the drawables as a key value pair using collection framework?

You can get the name of your resource by
String resourceName = getResources().getResourceName(resId);

You can store all the image ids in an array.
For example,
int images[]=new int[]{R.id.a,R.id.b,...,R.id.z};
Then you need to maintain the position in array of the current image every time you navigate.
For example change position as
pos=(pos+1)%26;
for next image and
pos=(pos-1)%26;
for previous image
and then use
setImageResource(images[pos]);
if its an ImageView or ImageButton, or using getResources().

You can retrive the ids of images in drawable folder like this...
int imagekey=getResources().getIdentifier(url.toLowerCase(),"drawable",getPackageName());
where url is the name of the images in the drawable folder

Related

how to insert image in text column in sqlite for android app

i have 4 cloumns in sqlite db (id,name,text,fav)
my android app is ebook.
i want add image to textview For example, I have 5 paragraphs in one lesson that I stored all those paragraphs in one cell of a row in db. Now I want to add an image after line 3 of first paragraph and 1 image after second paragraph. How can add images into the locations? In other lessons might be vary the locations.
please help me!how can add image between text!
If you want to add the actual image in the database you need to add
it as a blob, see this post
how to store Image as blob in Sqlite & how to retrieve it?
If you just want to add an image location then create a new column and use it to store the image location or resource id.
If you want to add a location for where in the layout to put the
image as well as the image location then create two columns; one for
storing the image location (or resource id) and one describing the
position of the image in the layout.
Remember that if you change the database structure (for example adding columns) you need to uninstall the app from the emulator/device or clear the cache in order to make it work. Otherwise you will get an error because the actual database structure wont change after it has been set once even if you change the structure in your code.

How to get the name of drawable through resource id?

I am trying to make the bitmap and for this I need the drawable name such as:
overlayScaledBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.monkey);
This is working fine But my problem is that I can not do this same thing when a user selects a picture from a list because I have done this statically. As you can see I have wrote R.drawable.monkey. So its means every time it is going to create bitmap with monkey image.
But it is not the case, the user selects image which gets fixed into the image view. Now I want to get the ImageView as drawable. If it is possible then it could be dynamically and would be easy to handle. Any idea How I can get drawable of ImageView to use in bitmap scaling? please help
You could create an enum object containing all refs to the bitmaps and an id.
But another way is using getTag() and setTag() on the resources to identify by.

Android: store image in database or drawable

In my app, there may be more than thousand of small image/ icon
and they will be display in views inside the list view or grid view
While I am wondering how the data should be store
1. covert to the string array and store in the database.
2. in the drawable folder (and how should the path store in the database?)
3. other better method?
A drawable resource is a general concept for a graphic that can be drawn to the screen and which you can retrieve with APIs such as getDrawable(int) or apply to another XML resource with attributes such as android:drawable and android:icon.
You should store the images in Drawable folder. The Drawable folder is pre-compiled. Which means that the images will be fetched much faster than from Database.

how to get what are the images are on the screen at runtime in android sdk

I don't know how to get the images what are available on the scree. I'm using imageview for the purpose of display the images on the screen.
Images will be changed randomly because i stored the images into array and images are in drawable. I want to know which image is displays on the screen at runtime.
thank u
When you change the image in the ImageView, hold onto the array index that you used in another variable.

How to swap images dynamically - wrong image showing

I need to change the source of an ImageView dynamically. I have a whole bunch of them with the same names, stored in their different dpi directories (drawable-hdpi etc) under res/. For this I've been using ImageView's setImageResource() and passing it a value from an array of resource IDs I've created at runtime like so:
decorations = new int[]{
R.drawable.bird1,
R.drawable.flower2,
R.drawable.bird3,
..etc};
Anyway, for some reason, the 6th image gets corrupted into an alternative image used for other things, not one in the resources list.
What on earth is going on?
Turns out I had a corrupt/incorrect image or the image was in the wrong directory or something. Still, strange that it decided to just display the next image in the directory rather than just crashing with an Exception or something.

Categories

Resources