How to Store image to SQLite in Xamarin Android? - android

Xamarin Android Application to Store Image in SQLite From Gallery(available images in the phone) .My Question is How to Image Storage in SQLite? And Retrieve Image From SQLite? Is possible only Image Compressing using bitmap function ?

The basic idea to store/fetch Image bitmaps in most of the DB is following,
Store: Convert the Image bitmap into a Base64String and store it to SQLite.
public static string Base64Encode(string plainText) {
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
return System.Convert.ToBase64String(plainTextBytes);
}
Retrieve: Fetch the Base64String and convert that to Bitmap again.
public static string Base64Decode(string base64EncodedData) {
var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
}

You can store your bitmap image as a byte[] to SQlite. For this operation you should convert bitmap to byte array. This piece of code, convert bitmap to byte[]
var memoryStream = new MemoryStream();
bitmap.Compress(Bitmap.CompressFormat.Png, 0, memoryStream);
imageByteArray = memoryStream.ToArray();

imageView.BuildDrawingCache (true);
Bitmap bitmap = imageView.GetDrawingCache (true);
BitmapDrawable drawable = (BitmapDrawable)imageView.GetDrawable();
Bitmap bitmap = drawable.GetBitmap();

Related

Converting base64 into bitmap and loading into recycler view

I'm trying to convert base64 image into bitmap image and then load it using picasso library into a recycler view. However, I get an error whenever I run my code saying I need to pass in a URI into the picasso method.
public String getImage(){
Context context =null;
byte[] decodedString = Base64.decode(image, Base64.URL_SAFE);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
decodedByte.compress(Bitmap.CompressFormat.PNG, 100, bytes);
path = MediaStore.Images.Media.insertImage(context.getContentResolver(), decodedByte, null, null);
return Uri.parse(path);
}
DataAdapter:
Picasso.with(context).load(data.get(i).getImage()).into(holder.reportImage);
Once you have downloaded and decoded a bitmap, there is no point of using an image loading library. All of the benefits (request queue and cache) are lost. You can simply use holder.reportImage.setImageBitmap(bmp);
Another approach would be to write a custom RequestHandler, that will decode the base64 data after downloading.
Assuming that you are using base64 image as a placeholder for your item in recycler view I suggest you use this method to convert base64 string to bitmap -
fun fromBase64ToBitmap(context: Context, base64string: String?): BitmapDrawable {
val decodedString = Base64.decode(string, Base64.DEFAULT)
val bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.size)
return BitmapDrawable(context.resources, bitmap)
}
and set
picasso.load(your_high_quality_image)
.placeholder(bitmapDrawableFromAboveMethod)
.into(imageHolder)
Hope this helps.
Note: Code is in Kotlin but you can easily convert it in java

Why is my BitmapFactory.decodeByteArray returning null?

I am trying to convert an image stored in Database in Base64 format, to a Bitmap to be used in an Imageview.
So, I store it in SQLite this way:
Bitmap imageBitmap = (Bitmap) extras.get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap fotoGrande=(Bitmap) extras.get("data");
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
//I am adding some data to EXIF here, add to an static ArrayList<Bitmap> in other class and I store it this way:
int bytes=listaFotos.get(i).getFoto().getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes);
listaFotos.get(i).getFoto().copyPixelsToBuffer(buffer);
values.put("foto", Base64.encodeToString(buffer.array(), Base64.DEFAULT));
Later, i need to get that image to fit it in an ImageView:
String foto = csr2.getString(0);//csr2 is a cursor
byte[] arrayFoto = Base64.decode(foto, Base64.DEFAULT);//This is not null
Bitmap fotoBitmap = BitmapFactory.decodeByteArray(arrayFoto, 0, arrayFoto.length);//This is null
I know there are tons of questions about this. I searched, but no answer fix my problem.
Why is my BitmapFactory.decodeByteArray returning null? What I am doing wrong? Any help?
Thank you.
Turned out to be a database issue. SQLite gives you a cursor of 1MB MAX size. I was getting the bytes from database, with a 1MB cursor, the pic was not sent properly.
To fix it, I stored the path to the photo in database instead of the bytes.
firstly image is convert in Bitmap to String like this
ByteArrayOutputStream stream = new ByteArrayOutputStream();
camera.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte imageInByte[] = stream.toByteArray();
String encodedImage = Base64.encodeToString(imageInByte, Base64.DEFAULT);
where camera is a Bitmap.
and store the String encodedImage in database
And getImage string like this
byte[] b = Base64.decode(encodedImage , Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);

how to convert base64 string image to blob in android?

I have a Base64 string image .I want to convert it into blob type and save it in database..
I have
String base64Image="iVBORw0KGgoAAAANSUhEUgAAAGAAAACgCAYAAADzcGmMAAAACSV...";
This is what I tried..
byte[] byteImage=org.apache.commons.codec.binary.Base64.decodeBase64(befImage.getBytes());
json.put("during_unloading_photo", byteImage);
where json is my JSONObject and
during_unloading_photo is the column name which is blob type.
for saving blob into DataBase you have to convert your base64 satring image to Byte[] and then you can save byte[] into db.
for converting base64 stringto bitmap
public static Bitmap decodeBase64Profile(String input) {
Bitmap bitmap = null;
if (input != null) {
byte[] decodedByte = Base64.decode(input, 0);
bitmap = BitmapFactory
.decodeByteArray(decodedByte, 0, decodedByte.length);
}
return bitmap;
}
and now you can convert this Bitmap to Byte[] Like:
public static byte[] getBytesFromBitmap(Bitmap bitmap) {
if (bitmap!=null) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream);
return stream.toByteArray();
}
return null;
}
then finally you can save this Byte[] to Db.make sure your column name have type with blob
enjoy your code:)-
getBytes gives you the bytes of the string, it does not do base64 decoding. You will need to use a Base64 decoder to do the conversion. A little search will show up several options you can use.

i want to save image in sql lite with name and status message and also retrieve it

i want to save image in sql lite with name and status message and also retrieve it.I want to save the image form the imageview which i get from the gallery and also retrieve it image view.I dont know how i save image in database
Insert an image into SQLite database, you need to use BLOB type
See this link :
http://tttrung43.wordpress.com/2012/04/12/store-image-in-sqlite-database/
http://xjaphx.wordpress.com/2011/06/25/insert-image-to-database/
Convert image to Base64 String and save in database table with columns such as img_name, status, img
Checkout Base64 class in android
public static String convertImageToBase64(Bitmap img)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
img.compress(Bitmap.CompressFormat.JPEG, 90, baos);
byte[] byteArrayImage = baos.toByteArray();
return Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
}
public static Bitmap convertBase64ToImage(String base64)
{
byte[] byteArray = Base64.decode(base64, Base64.DEFAULT);
return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
}
please refer below links for sqlite
http://www.vogella.com/articles/AndroidSQLite/article.html
http://developer.android.com/reference/android/database/sqlite/package-summary.html

Decoded String Image Can't Be Display As BitMap in Android

Below is the portion of code I used to encode the image file from bitmap to base64 string by import android.util.Base64 in my android project:
`
Bitmap img_bmp=BitmapFactory.decodeStream(getContentResolver().
openInputStream(this.browseImageURI));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
img_bmp.compress(Bitmap.CompressFormat.JPEG, 30, baos);
byte[] image = baos.toByteArray();
String profile_img = Base64.encodeToString(image, Base64.DEFAULT);
`
The string profile_img will be saved in mysql database as a string.
When I retrieve the string value from database, I will decode image string from String to Bitmap by using the below code:
`
Intent i = getIntent(); //pass the value from previous activity
str_img= i.getStringExtra("img");
img_bm = StringToBitMap(str_img);
imgview = (ImageView)findViewById(R.id.imageView1);
imgview.setImageBitmap(img_bm); // display the image
//Function to convert string to bitmap
public Bitmap StringToBitMap(String image){
try{
byte [] encodeByte=Base64.decode(image,Base64.DEFAULT);
Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
return bitmap;
}catch(Exception e){
e.getMessage();
return null;
}
}
`
I expected it will display the image, however the image is not displayed and I get a log message from the Base64 decoder "--- decoder->decode returned false".
Can someone help me to figure out what is wrong in my code?
And do anyone know how to convert the base64 string image (pass from JSON to php script) into a blob format so that I can store it as BLOB in mysql.
Thank you in advance.

Categories

Resources