Android: Image loaded from binary data in DB will not display - java

I'm developing an app in which a user can upload an image as part of a short blog post; this image is then base64 encoded and stored in a MS SQL Server database. When I try to view the image later, it doesn't display. I have a placeholder in the ImageView which disappears when an image is there to be loaded but no image actually displays.
Encoding image:
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
img.compress(Bitmap.CompressFormat.JPEG, 35, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
String image = Base64.encodeToString(byteArray, Base64.DEFAULT);
Decoding image and putting into ImageView:
byte[] decodeString = Base64.decode(blog.getImage(), Base64.DEFAULT);
Bitmap decodebitmap = BitmapFactory.decodeByteArray(decodeString, 0, decodeString.length);
image1.setImageBitmap(decodebitmap);
Any help with where I'm going wrong is greatly appreciated.

Use this function to get Bitmap from base64
public static Bitmap decodeBase64(String input)
{
byte[] decodedBytes = Base64.decode(input, 0);
return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
}
This function will set the above bitmap to ImageView
private void setImage(ImageView iv,Bitmap bitmap)
{
iv.setImageBitmap(bitmap);
}

Related

Trying to display image retrived from server with base64 encoding but cant display the correct image

These are my encoding and decoding methods.
public String encodeBase64(Bitmap bit){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bit.compress(Bitmap.CompressFormat.PNG, 100, baos); // Could be Bitmap.CompressFormat.PNG or Bitmap.CompressFormat.WEBP
byte[] bai = baos.toByteArray();
return Base64.encodeToString(bai, Base64.DEFAULT);
}
public Bitmap decodeBase64(String base64Image){
byte[] data = Base64.decode(base64Image, Base64.DEFAULT);
Bitmap bm;
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inMutable = true;
bm = BitmapFactory.decodeByteArray(data, 0, data.length, opt);
return bm;
}
This is encoded data from a bitmap
iVBORw0KGgoAAAANSUhEUgAABLAAAAdcCAYAAAC7T3KrAAAABHNCSVQICAgIfAhkiAAAIABJREFU
eJzswQEBAAAAgJD+r+4ICgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.... etc
But the data i get from the server looks like this
{"type":"Buffer","data":[105,86,66,79,82,119,48,75,71,103,111,65,65,65,... etc
I can get pure data or the type and data json object. Ive tried to decode both. When i try to decode with the type and data it gives me a null bitmap, when i decode the raw data only the image is not displayed, but nothing seems to crash.
Do i need to retrive all those array numbers and convert them to chars before decoding? Ive read other post and it sounded very straigtforward, just encode and then decode.
decode base_64 string Imgae in codigneter
$
aadharcard_front = image name(database)
if(!empty($aadharcard_front))
{
$aadharcard_front = base64_decode($aadharcard_front);
$aadharcard_front_time = time().'.png';
file_put_contents(APPPATH.'../media/uploads/'.$aadharcard_front_time,$aadharcard_front);
$user_data['aadharcard_front'] = $aadharcard_front_time;
}

Convert .gif image to base64 to upload to server

I'm trying to encode an animated gif to base64, so I can upload it to server. I am already doing it with static images, but if I try to do it with GIF, it simply does not work. I get the image from Intent image picker and put it inside an ImageView.
Here is the code I use to encode images to base64:
Bitmap bitmap = ((BitmapDrawable) ((ImageView) nextChild).getDrawable()).getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 60, baos);
byte[] imginbyte = baos.toByteArray();
value = Base64.encodeToString(imginbyte, Base64.DEFAULT);
try this :
byte[] imginbyte = Base64.decodeBase64(bitmap.getImageBase64());
new FileOutputStream("image2.gif");
write(imginbyte);
close();

Android+Java get image from URL on server and pass as string to client: Base64 decode and encode not working

In Java server I fetch image from external service URL like:
InputStream in = new java.net.URL(imageWebServiceURL).openStream();
String resultToCleint = org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString(IOUtils.toByteArray(in));
Then on Android I parse it like:
byte[] imageAsBytes = Base64.decode(resultToCleint.getBytes(), Base64.DEFAULT);
imageView.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length));
Result: Image not displayed, ain't errors/exceptions neither on server nor on client.
What is the problem here?
EDIT: On android I use class android.util.Base64
Thanks,
Use Picasso library to load image:
You just need to add 1 line of code to show the image on ImageView
//Loading image from below url into imageView
Picasso.with(this)
.load("YOUR IMAGE URL HERE")
.into(imageView);
You can learn more from here
use this to convert to base 64
public static String uploadPic(Bitmap bm) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
String encoded = ""+ Base64.encodeToString(byteArray, Base64.DEFAULT);
return encoded;
}
check if image is uploaded then using volley String request object download the string response using this code convert it back.
public Bitmap StringToBitMap(String encodedString){
try {
byte [] encodeByte=Base64.decode(encodedString,Base64.DEFAULT);
Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
return bitmap;
} catch(Exception e) {
e.getMessage();
return null;
}
}
As commented, let's assume base64Content is the base64 string responsed from your web service/server-side app, you can refer to the following sample code:
String base64Content = jsonObject.getString("Base64Content");
byte[] bytes = Base64.decode(base64Content, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
Moreover, if your server compressed reponse data either by gzip or deflate, your client app must decompress the data first.
Hope this helps!

Why can't I decode Android encoding image using base64String with an other decoder?

I have an image that I encode using this code :
Bitmap bm = BitmapFactory.decodeFile(selectedImagePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
It encodes well. After that I'm getting the String in a file in my computer and I try to decode it with this website :
http://www.askapache.com/online-tools/base64-image-converter/
And I'm getting a null picture. However, when I decode it directly in my app and display it like :
public static Bitmap decodeBase64(String input) {
byte[] decodedByte = Base64.decode(input, 0);
return BitmapFactory
.decodeByteArray(decodedByte, 0, decodedByte.length);
}
private ImageView img;
img.setImageBitmap(decodeBase64(encodedImage));
I'm getting the right picture. So I was wondering if i actually can't decode an android encoded image with an other decoder than android's one or my file is corrupted meaning that I have missing characters. However even if I don't have the whole encoded file, the decoded picture i'm getting with the website should look like to the good one right ?
Thank you in advance

encoded image in base64 back to image

so I am trying to take a string for an image I encoded using base64 and turn it back into an image I can use in an ImageView. The code to encode it is:
final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(), options);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
image_str = Base64.encodeToString(b, Base64.DEFAULT);
I'm assuming it would be convert image_str back to a byteArray then back to bitmap?
I am not very familiar with base64 functions so I figured I would ask here while I search, to get more done in the same amount of time.
Thank you in advance,
Tyler
EDIT: I did find this bit of code but the image does not show up and logcat says decode returned false:
byte[] imageBytes=Base64.decode(imageString,Base64.NO_WRAP);
InputStream in = new ByteArrayInputStream(imageBytes);
Bitmap b = BitmapFactory.decodeStream(in);
You'd first decode the Base64 encoded string to bytes:
byte[] decodedBytes = Base64.decode(image_str, Base64.DEFAULT);
Then convert the bytes back to a JPG:
Bitmap bm = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);

Categories

Resources