0

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.

Vedvart1
  • 302
  • 4
  • 21
  • Possible duplicate of [How to convert image into byte array and byte array to base64 String in android?](https://stackoverflow.com/questions/10513976/how-to-convert-image-into-byte-array-and-byte-array-to-base64-string-in-android) – Northern Poet Oct 30 '17 at 00:20
  • @NorthernPoet that question starts with a file path for an image; I don't have the image locally stored, I only have an Image object. – Vedvart1 Oct 30 '17 at 00:21
  • @Vedvart1 where your image is located? in drawable folder? or you are loading from web url? – Lokesh Desai Oct 30 '17 at 04:33
  • `The image is stored solely as an instance of android.media.Image, `. Where is it stored that way? – greenapps Oct 30 '17 at 08:53
  • @LokeshDesai They aren't stored on the phone or loaded from a URL; they are stored in an online database as strings. See my new edit. – Vedvart1 Oct 30 '17 at 20:45
  • @Vedvart1 your image is loaded in imageview? – Lokesh Desai Oct 31 '17 at 00:23
  • @LokeshDesai I plan on doing that once it's an Image object. Unfortunately I can't load the Base64 strings into an imageview straight from Firebase. – Vedvart1 Oct 31 '17 at 01:30
  • Let us go step by step.first i will answer you how to load base64 string into imageview – Lokesh Desai Oct 31 '17 at 01:53
  • @Vedvart1 check my answer inform me if u face any issue – Lokesh Desai Oct 31 '17 at 01:56

1 Answers1

3

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);
Lokesh Desai
  • 2,607
  • 16
  • 28