i have images stored as varbinary in SQL table, how can display the images in android client
Asked
Active
Viewed 451 times
-1
-
What is the format of the data stored in the `Image` field? Please provide a **complete sample** as text – Forntoh Mar 28 '20 at 10:06
1 Answers
0
What you need to do is
- Convert the
Image
string from DB to aByteArray
- Use
BitmapFactory
in android to convert fromByteArray
toBitmap
- Set the bitmap of your imageview
val imageText = "FFDD8FFE000104A4694600010101000000000000FFEE1138..."
myImageView.setImageBitmap(getBitmap(imageText))
You could use the following function [kotlin]
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import com.google.android.gms.common.util.Hex
/**
* Converts a hex string to Bitmap
*
* @param image hex string e.g. "FFD8FFE0..."
* @return Bitmap
*/
fun getBitmap(image: String): Bitmap {
// Convert String to ByteArray
val byteArray = Hex.stringToBytes(image)
// Convert ByteArray Bitmap
return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.size)
}

Forntoh
- 311
- 4
- 7
-
i get the error `Caused by: java.lang.IllegalArgumentException: bad base-64` – johnzilla Mar 27 '20 at 21:23
-
Make sure that you use `FF...` instead of `0xFF..`, If your strings have the header please remove it – Forntoh Mar 28 '20 at 10:26