How to convert a Base64 string into a Bitmap image to show it in a ImageView?
You can just basically revert your code using some other built in methods.
byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
Convert base64 string to image in Android
//encode image(from image path) to base64 string
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Bitmap bitmap = BitmapFactory.decodeFile(pathOfYourImage);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT);
//encode image(image from drawable) to base64 string
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.yourDrawableImage);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT);
Convert base64 string to PNG image in android
Use this
byte[] decodedString = Base64.decode(imageString, Base64.DEFAULT);
// Bitmap Image
Bitmap bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
String filename = "MyImage.png";
File file= Environment.getExternalStorageDirectory();
File dest = new File(file, filename);
try {
FileOutputStream out = new FileOutputStream(dest);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
Required Permission in AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
How to convert base64 string into image in kotlin android
You can use this code to decode: -
val imageBytes = Base64.decode(base64String, Base64.DEFAULT)
val decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length)
imageView.setImageBitmap(decodedImage)
Android code to convert base64 string to bitmap
Assuming that your image data is in a String called myImageData, the following should do the trick:
byte[] imageAsBytes = Base64.decode(myImageData.getBytes(), Base64.DEFAULT);
ImageView image = (ImageView)this.findViewById(R.id.ImageView);
image.setImageBitmap(
BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
);
For Base64 decoding, you can use http://iharder.sourceforge.net/current/java/base64/ as Android doesn't contain Base64-support prior to 2.2.
Note, I didn't actually run this code, so you'll have to doublecheck for errors.
Converting base64 imageString to bitmap to display in ImageView showing null in bitmap
03-24 04:29:43.816: D/skia(15392): --- SkImageDecoder::Factory returned null
This signals that the input to BitmapFactory.decode
was invalid in some way.
Below I list a few possible scenarios that may trick you, not sure which one applies though, because you didn't disclose the real Url, and the code you posted doesn't even try to decode data coming from the network.
Base64 Padding
Your hardcoded input string is GvygDaYb64wUon0lxp2H1458543376
which is not a valid Base64 encoded string. To see why enter it on the page: http://string-functions.com/base64decode.aspx, it'll say:
Invalid length for a Base-64 char array.
Change it to GvygDaYb64wUon0lxp2H1458543376==
(notice the end) and it'll show you weird glyphs, but that's just because it's binary data, so it successfully decoded.
Weirdly the Base64
class should throw an exception when padding is missing, which you didn't get...
You can also check the contents of an encoded string in the Chrome, enter this into the address-bar of a new tab (replace after comma, see valid example):
data:image/png;base64,GvygDaYb64wUon0lxp2H1458543376==
I see a little square, which is likely not the image (see next section).
Base64 Format
That sample you're trying to decode is also in some weird format. It's not a PNG/GIF/JPEG file which is supported by the Android system, it's missing a header. Take a look at the list of supported formats: http://developer.android.com/guide/appendix/media-formats.html (scroll down to Images), these are the data types you'll be able to load.
I tried loading the that short byte[] with multiple imaging software on my laptop and nothing recognized it as an image file and I agree with them, I can't see any image-like in it in a hex editor either.
Base64 - Not!
If you're trying to decode the variable response
make sure it's a Base64 encoded string. What is in the logs above is a JPEG file, but it's probably broken. Notice that you're using StringRequest, that will only work if you're really getting a Base64 string which consists of [a-zA-Z0-9+/=]
characters.
To read binary data you'll need to fire a different type of request which is different based on your networking library, but the key point is that if you receive binary data in String format that means there was some text encoding applied to it, but binary data doesn't have character encoding, it's just plain 0x00-0xFF bytes without any interpretation.
BitmapFactory.decode
on binary data will only work if you're receiving a byte[]
from the network library or an InputStream
(Reader
s also have text encoding associated which is wrong for binary data).
Set a bitmap image in ImageView
Try doing this after initializing the Bitmap decodedByte
BitmapDrawable drawable = new BitmapDrawable(getResources(), decodedByte);
holder.imageView.setBackgroundDrawable(drawable);
EDIT: Try this:
String base64Image = product.getImage().split(",")[1];
byte[] decodedString = Base64.decode(base64Image, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
holder.imageView.setImageBitmap(decodedByte);
Hope this helped!
Related Topics
Using Application Context Everywhere
How to Obtain Crash-Data from My Android Application
Sending Data Back to the Main Activity in Android
How to Change the Background Color of the Actionbar of an Actionbaractivity Using Xml
What to Do on Transactiontoolargeexception
How to Use the Textwatcher Class in Android
How to Detect Incoming Calls, in an Android Device
Simple Android Grid Example Using Recyclerview With Gridlayoutmanager (Like the Old Gridview)
Firebase Onmessagereceived Not Called When App in Background
How to Check a Certain Data Already Exists in Firestore or Not
Android Sqlite Database: Slow Insertion
Volley Jsonobjectrequest Post Request Not Working
Environment.Getexternalstoragedirectory() Deprecated in API Level 29 Java
Android How to Use Mediascannerconnection Scanfile
How to Handle Screen Orientation Change When Progress Dialog and Background Thread Active