I'm wondering what the best way to store a local phone contact's icon is. I'm writing a manager that will allow the user to select contacts for display in a list which now needs contact icon display.
Would storing the icon in a different location on select work or should I try the route of storing the location of the icon and linking to it that way? Can anyone who has went through this already point me in the right direction? I'm using an Sqlite database to store the contacts already.
Any code/links would be very helpful.
Since you are already storing the contacts in an Sqlite db, I'd just add another field to that db that will hold an encoded image.
The way I have gone to solve a similar issue is: I used Base64 for encoding an image into a String and then I store that String wherever I want...
I added one function to the Base64 class to directly encode a Bitmap object for me and return a String, here's the code:
public static String encodeBitmap(Bitmap bmp) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] buf = stream.toByteArray();
return encodeBytes(buf);
}
where encodeBytes(buffer) is already an implemented function of the Base64 class.
This would be a better way of doing it than storing the path to the image, because a user can easily change the path and then your application wouldn't find the picture anymore.
Hope that helps.
First, I'd recommend looking at the Android source and seeing how they do it.
That aside, if the images are smallish, I'd highly recommend creating a proper ContentProvider and storing the images as binary blobs attached to the row using Cursor.getBlob(). See http://developer.android.com/intl/de/guide/topics/providers/content-providers.html#querying for more details.
For larger images, take a look at How to store large blobs in an android content provider?
Related
I'm making a simple Android application to show all the countries and their flags. I want to use Room and SQLite to store the flag images but I dont really know which is the best way.
I can store the Flag images as names such as "france" and later retrieve them with:
getResources().getIdentifier("france","drawable",requireActivity().getPackageName())
But I am warned that this will be really slow and with hundreds of countries the app wont be good. At the moment I have all the flag icons in the res/drawables folder, so what is the best way to save them into the database and retrieve later? Thank you.
If You want to store drawables in Room DB, You have to convert it into byteArray.
Drawable d;
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitmapdata = stream.toByteArray();
define column in room as byte[].
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 ....
Recently I was working on MySql and Sql, where I found I can almost insert everything primitive type data but is it possible to store a jpeg/jpg/bmp/png (image)file in a database. If so then can you please write the code. Don't need to explain the whole thing just the key point.
Thanks
1 Perhaps convert the image to a Byte Array (if it's not bmp, then convert it), then store it in MySql as a Blob type, then when reading it, you can create a Bitmap through the code (I don't know what language you're using) with the Byte Array with what you read, or perhaps just get the code associated with the image and store that and read it.
Converting an image to a Byte Array
2 Use an image hosting API (like Imgur) and have the user's image upload to that site, and just read the URI from the database whenever you want to use it.
Imgur API, Android Example
there is 3 cases how to make it (that is i know)
Save path from SD card like String in database
Save int value if image is in res folder of your app.
Save url like String if image is in Internet.
Sorry for the late response. However, I found a way to convert the image to String and then store it in the mysql.
This answer is just for those who are still looking for a easy solution:
code:
Bitmap bm = BitmapFactory.decodeFile("/path/to/myImage.jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
and then:
String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
Then the encodedImage which is a String format, I can insert into the db.
Hope that helps other who are still looking for the answer.
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 );
My app currently requests a JSON file with some text and other data from my server. I want to add functionality so that it also downloads a very small image (like an icon) through the same file [without creating an additional request]. Is it possible to do so, and how would I go about it (base64?)
Should be eminently reasonable: look at http://developer.android.com/reference/android/util/Base64.html. All you'd need to do is:
Read your icon into a byte[] array on the server.
(Assuming your server is in java) Use something like http://iharder.sourceforge.net/current/java/base64/ to write the byte[] array into a StringOutputStream through http://iharder.sourceforge.net/current/java/base64/api/index.html?Base64.OutputStream.html.
Add the contents of the String to the JSON file.
On the android device call http://developer.android.com/reference/android/util/Base64.html#decode%28java.lang.String,%20int%29 to convert the JSON attribute into a byte[] array.
You can then pass the byte array to http://developer.android.com/reference/android/graphics/BitmapFactory.html#decodeByteArray%28byte[],%20int,%20int,%20android.graphics.BitmapFactory.Options%29 or one of its brethren functions (you may have to play with image formats/encodings to get it to swallow your byte array correctly).
Voila! You have a Bitmap you can use.
Let me know how that works out.