1

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 ?

Sagar Zala
  • 4,854
  • 9
  • 34
  • 62
Aboobakkar P S
  • 796
  • 1
  • 8
  • 28

3 Answers3

1

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);
}
fluffyBatman
  • 6,524
  • 3
  • 24
  • 25
  • is possible image filepath into SQLite ? `File imgFile = new File("/storage/emulated/0/Pictures/CameraAppDemo/myPhoto_2eb3b135-5ad5-4a17-8836-13a9eca6bbb2.jpg"); Bitmap myBitmap = BitmapFactory.DecodeFile(imgFile.AbsolutePath);` – Aboobakkar P S Aug 25 '16 at 12:48
  • It's a common practice in server side technology. Might as well as possible for android. One thing though, if user deletes the photo manually then the path will become invalid. So, you gotta put another layer of checking in there. – fluffyBatman Aug 26 '16 at 06:12
0

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();
Onur
  • 247
  • 3
  • 13
0
imageView.BuildDrawingCache (true);
Bitmap bitmap = imageView.GetDrawingCache (true);
BitmapDrawable drawable = (BitmapDrawable)imageView.GetDrawable();
Bitmap bitmap = drawable.GetBitmap();
omid-ahmadpour
  • 63
  • 1
  • 12