Does resource id changes everytime an application starts - android

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

Related

Storing image id into SQLite db but cannot retrieving it?

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.

Save Image and Text together in database on android

I am working on an Android app like Notepad.
There are two sections.
Text Only
Text with images
A notepad which can only get text from user and save it in db and then retrieve is done.
The another section, will work basically like MS Word.
User can select image from gallery and save it in notepad.
But how can I achieve it? Because I want to store that whole data (image & text) into db as it is. Means suppose user added an image after 2 lines of text. Then app should retrieve it from db in that format only.
Any suggestions? Please help me. Thanks in advance.
The most simple solution would be to define tags or some other form of markup language, that would store that image within a text as a path to file on Disk for example which is imho better alternative than storing it in SQLite but if you insist on database, you can store something like an imageId and keep it the there... However, you are manually doing something, that has already been optimized for you when trying to handle the byte array instead of a file in memory ..
Once you store images in this custom markup language, you can parse it and split the text on chunks between the images which you populate dynamically to some ViewGroup(i.e. LinearLayout) as separate TextViews and ImageViews...
Finally, if you want a solution quickly, you can use some already developed libraries such as this : https://android-arsenal.com/details/1/1696 ... However, this pattern is so common that you can find a great number of libraries that do exactly the thing you are looking for ...
Don't reinvent the wheel if it's not necessary..
You can save the byteArray or uri of images in the database.
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); byte[] byteArray = stream.toByteArray();
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
Uri uri = Uri.parse(path);
Hope this helps ....

Converting Multiple Picture to byte array in Android

i have one questions, how to convert Multiple picture to byte array (byte []), cause i have case to save many picture in my database sqlite, i have array list which contains picture in drawable folder..
ArrayList<Integer> imageId = new ArrayList<Integer>();
imageId.add(R.drawable.a1);
imageId.add(R.drawable.a2);
imageId.add(R.drawable.a3);
imageId.add(R.drawable.a4);
imageId.add(R.drawable.a5);
Then i have tried this code to convert into byte arrray
Bitmap b = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.a1);
//calculate how many bytes our image consists of.
int bytes = b.getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes); //Create a new buffer
b.copyPixelsToBuffer(buffer); //Move the byte data to the buffer
byte[] array = buffer.array();
System.out.println(array);
The code below is working, but the problem is how the code below is convert one picture, and now i need to convert multiple picture in arrayList, can anybody help me? cause i have tried with looping, it give me an error, java.lang.OutOfMemoryError..
Ok,
first of all do not save whole pictures to SqLite. It is not a suitable way. There's a very easy solution but a little risky..
Just save images to SD Card as WhatsApp do and save their path to SqLite. So whenever you want to access your pictures you can read their paths from Sqlite and access them.
But the risk is pictures are in user control, so they can remove it. But you can hide the pictures in a deep way :)

Getting an image from an HttpResponse

I'm attempting to hit a web service to retrieve an image pertaining to an ID I send with the request. The web service returns a jpeg.
I have a few questions:
I want to store this jpeg in my application's SQLite3 database. What column type should I use? A blob?
What data type should I read it in as to store it in the column?
And lastly, how do I take the data from this column and throw it into an ImageView?
I would suggest against using the DB , you can just use the cache folder to store the images ans perform normal file functions on it. Here is some info : http://developer.android.com/guide/topics/data/data-storage.html#InternalCache
Depending on the size of the image, you might want to store it instead on the SDCard (this is what Android does with images ... pictures you take, for example) and store only the Uri of that image in the DB. Small images you can store the bytes in the DB.
This is what I do, and it's worked well.
To create an image from the bytes you can use
Bitmap theImageFromByteArray = BitmapFactory.decodeByteArray( imageByteArray, 0, imageByteArray.length );
imageView.setImageBitmap( theImageFromByteArray );

How do I decode base64 blob to bitmap Android?

I have a Sqlite database in which a base64 code string is stored as a BLOB. I want to fetch this BLOB from the db and decode the base64 and show the image in an ImageView.
I have read about encoding and decoding base64 and this is what I am using now.
String[] image = new String[100000];
if (cursor.moveToFirst()){
int i = 0;
do {image[i] = cursor.getString(1);
i++;}
while (cursor.moveToNext());
String imagebyte = image[0];
byte[] decodedString = Base64.decode(imagebyte, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
mImage.setImageBitmap(decodedByte);
But nothing shows up and I don't get any error messages. In order to pinpoint the problem I tried encoding and decoding an image, and then the bitmap showed. Is this the right way to decode it? I am here using cursor.getString(1), I have tried with using getBlob(1) also, but still nothing shows. Should I use getString or getBlob?
Is there an easy way to check validity of the base64 code? THanks!
I have a Sqlite database in which a base64 code string is stored as a BLOB.
Why? Why not just put the image in the BLOB? What are you gaining by Base64-encoding it? It is costing you CPU time, garbage collection time, and storage space.
String[] image = new String[100000];
Why do you think you need 100,000 String objects?
do {image[i] = cursor.getString(1);
i++;}
while (cursor.moveToNext());
Why are you retrieving this column for all rows in your result set, when you are only using one of them? Particularly considering you are obviously doing all of this on the main application thread, and therefore you will eventually crash with an application-not-responding (ANR) error?
Also, if this column is declared in your database a BLOB, are you sure that getString() will work as you expect?
Is this the right way to decode it?
That would depend upon how you encoded it.
Should I use getString or getBlob?
That depends on how you put the data in the table and what data type the column has. They should all match.
But I would just get rid of all the Base64 stuff and store the image byte array in the BLOB column, if you want an image in a table.

Categories

Resources