Storing image id into SQLite db but cannot retrieving it? - android

I have a question. I know I can store an image as byte array in SQLite database. But this needs some extra CPU work, so I tried to store only the id (R.drawable.myImage).
I stored it as an integer and I can retrieve it back. I am setting my image into imageview like this:
ivSportIcon.setImageResource(getItem(i).getIcon());
But I get an error:
Resources$NotFoundException: Resource ID #0x8
Is this a wrong way? Should I store my image as a byte array? Thank you in advance!

I do not know how you are storing the resource ID, but 0x8 is not a resource ID.
Beyond that, bear in mind that resource IDs change with each build, and so the resource ID that you save today will be different tomorrow.
Instead, store some other identifier in the database, that you can use to decide which of your drawables to use.
The only reason to store the actual image data in the database would be if you plan to change the images in the future and you want the user to be able to use the old images that happen to be associated with database entries.

You could store the name of the Drawable as a String instead, and use this method to retrieve its id:
public static int getResourceIdForName(Context context, String drawableName) {
final Resources res = context.getResources();
final String packageName = context.getPackageName();
return res.getIdentifier(drawableName , "drawable", packageName);
}
This is a little inflexible however, as if you decide to change the name of your drawable in future, then you will also need to change the name in the database.

Related

android integer text as ressource

is it possible to avoid storing images in a database and putting them into the drawable or mipmap folders instead? In the database then I would like to put only the reference to ressources and call it like this:
String image = "R.mipmap.image1"; //This will be the data retrieved from Database
int imageInt = Integer.parseInt(image);
imgview.setImageRessource(imageInt);
Actually I want to use this in a RecyclerView so I simplified the example code here but this is showing me the following error
java.lang.NumberFormatException: Invalid int: "R.mipmap.image1"
Thanks
One simple way is to store the image name in database (i.e. "image1" in your example). Then later get the identifier for it using image name as below:
int imageId=context.getResources().getIdentifier("imageName","mipmap",context.getPackageName());
Now, you can use this imageId for setting image to image view.
imgview.setImageRessource(imageId);

Does resource id changes everytime an application starts

I am storing my images in drawable and their resource id in SQLite database.My database is created when the application starts for the first time.
Is it good to save the image ids in database or does the id's change every time an application start?
if id's change then while fetching back image id's I may receive an error.So,is storing image ID in database a good idea?
i need the images path to be stored in database with other relative data that's why i am storing the image id's in data base.
One approach would be storing the drawables in strings.xml as a string array something like this:
<string-array name="location_flags">
<item>#drawable/ic_image_name</item>
<item>#drawable/ic_image_name</item>
<item>#drawable/ic_image_name</item>
<item>#drawable/ic_image_name</item>
<item>#drawable/ic_image_name</item>
<item>#drawable/ic_image_name</item>
<item>#drawable/ic_image_name</item>
</string-array>
Then reading this array in your activity code :
TypedArray locationFlags=getResources().obtainTypedArray(R.array.location_flags);
Then applying the for loop you can get the Drawable something like this:
for(int i=0i<locationFlags.length();i++)
{
Drawable drawable = locationFlags.getResourceId(i, -1);
}
Be sure to recycle the TypedArray after using it, since its a shared resource :
locationFlags.recycle();
the best way was to store image name directly into database and fetch it,then use
int resID = this.getResources().getIdentifier("your photo name fetched from database","drawable","package name");
image.setResourceID(resID);
It is Bad idea to store ids, But what is your purpose??
better idea is to store thumbnails to database rather than ids..
this helped me
may this example help you
do following steps
get thumbnails of your image
final int thumbnailSize = 128;// define your size
Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(imagePath),
THUMBSIZE, THUMBSIZE);
Convert it into Byte array
Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.thumbnail);
ByteArrayOutputStream out = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, out);
byte[] buffer=out.toByteArray();
Save it as Blob
ContentValues cv=new ContentValues();
cv.put(CHUNK, buffer); //CHUNK blob type field of your table
long rawId=database.insert(TABLE, null, cv); //TABLE table name
I havnt used this but this may help you

Best way to store Resource id in Database on Android

In my app i want to be able to store resource id of some icons in database. I suppose it is not save to do, cause in the later stages (application upgrades) i may want to add new icons to the app, so all id's may change? What is the best practice to store resource in case i do not want to store icons as blobs?
As an example, let's say i have a game 1.0, where i let user to choose an avatar for his character from a list of avatars. I want to store this avatars id in DB and be sure that if i add more avatars in game 1.1 - user will still see his choice.
The best way (in my opinion) is to store the resource entry name.
String iconName = getResources().getResourceEntryName(R.drawable.your_icon);
So your SQLite database column where you want to store the icon will have type TEXT. You're right that storing resource IDs is a bad practice because they're recreated when you compile the app, so you can't rely on them not changing (I learned this the hard way, all my app's icons got messed up).
To get the resource ID from the String when you need it, call
int resID = getResources().getIdentifier(iconName, "drawable", getPackageName());
Note that both getResources() and getPackageName() are methods from Context, so you need to have reference to your application/activity Context to be able to call these methods.

ViewImage change based on user input

i am currently trying change a imageView in my program, it needs to changed based what the user picks from the screen before, so i was wondering how i can access an image in my drawable folder through a string path?
all the examples i have seen have been either hard coded (R.drawable.XXX) or asking for the integer id value
Cheers
Make a HashMap using your string as a key and ID as a value.

Are string resource ID values guaranteed to be consistent over different projects?

I have some messages being passed back from my server through php. The problem is that the messages are in English and if the user is using another language they will still get the message in English.
So I had an idea that maybe instead of passing back the message I would instead pass the String resource Id from the android app, that way the app will get the correct string id for their language. I will use this in a number of apps so I just want to know if the string id is guaranteed to be the same across different android projects?
No.
The string resource IDs are likely not even guaranteed to be the same between re-compilations of the same application (e.g. differences between versions of aapt).
Christopher is right, but you can pass a parameter to your server like http://example.com/script.php?lang=en or lang=fr ....
This way the php scripts can use this parameter to return messages in the relevant locale.
You can get the resource ID from a string:
int resID = getResources().getIdentifier("org.my.package:strings/my_string", null, null);
// or
int resID = getResources().getIdentifier("my_string", "strings", "org.my.package");

Categories

Resources