how to insert an image in mysql -android - android

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.

Related

Store and fetch image in SQLite Android

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[].

Convert Image object to Base64 String in Android

I've seen a huge number of related questions, but none of them actually answer the question, and many just use code snippets out of context with undefined variables.
I need to store images in a database (which to the best of my knowledge I should do using Base64 encoded strings), and use them as Image objects in the android code.
From what I've worked out, I need to convert the Image to a Bitmap, then Bitmap can be turned into a Base64 string. But I can't for the life of me work out how to convert the Image into a Bitmap. Any help is appreciated.
EDIT: While other questions do convert an image from a file location to a string, I don't have a file location. The image is stored solely as an instance of android.media.Image, and I don't know how to access that using a file path or anything similar.
EDIT 2: Okay, so here's my setup: the images will be stored in Firebase database as Base64 encoded strings. When I need them, I will pull the string from there and convert it into an Image object, which is just a variable I have temporarily. They are never stored locally, the only time they're on the actual app they are the Image object.
Just convert your base64 string into bitmap than load that bitmap into imageview using below code
byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
image.setImageBitmap(decodedByte);

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 ....

Technique for Storing Android Contact Icons

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?

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