create imageview from base64.decode - java

I have this problem: I want represent a base64 string to image that come from php page in my android application (the string base64 work fine and come good) so I have done this:
byte[] gzipBuff = Base64.decode(json_data.getString("immagine"),0);
ByteArrayInputStream memstream = new ByteArrayInputStream(gzipBuff, 0, gzipBuff.length);
ByteArrayOutputStream baos = new ByteArrayOutputStream(gzipBuff.length);
baos.write(gzipBuff);
Bitmap bmp = BitmapFactory.decodeStream(memstream);
ImageView image = new ImageView(this);
image.setImageBitmap(bmp);
I don't see the imageview

A quick Google search finds one of your other duplicate questions as the first result, and this as the second. See the accepted answer from that question.

Related

"SkImageDecoder::Factory returned null " when trying to decode byte array into imageView - Android

In my app I try to store an image from an imageView as a byte array and store that in a databse
ImageView imageView = (ImageView) findViewById(R.id.imgpreview);
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] byteArray = baos.toByteArray();
This byteArray is then stored in a database, and extracted back out inside a custom GridAdapter so that the byte array can be converted back to a bitmap and set as the image view.
ImageView image = (ImageView)gridView.findViewById(R.id.image1);
TextView description = (TextView)gridView.findViewById(R.id.tv1);
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray.get(position), 0, byteArray.get(position).length);
image.setImageBitmap(bmp);
It doesn't seem to be a problem with storing or retrieving the byte array, as I log a toString of the byte array just before it attempts to decode it and it appears to be correctly logged, so I'm confused as to why it's being shown as null, the exact message is:
D/skia: --- SkImageDecoder::Factory returned null
I've looked through all the threads with solutions I could find and tried them all, I just can't figure out how to fix it. I've tried with varying image sizes. If any more info is needed let me know, thanks in advance.

Display PNG Byte Array in ImageView

Afternoon all.
I am trying to display a PNG image in an ImageView on my mobile android application. The image comes in the form of a byte array from a database.
I'm new to android development, so I'm not sure as to the correct way to go about this. I have tried saving the file and setting the image using the URI but to no success.
File tempFile = File.createTempFile("NewsImage", lastNewsDTO.ImageExt, null);
FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(lastNewsDTO.Image)
((ImageView)rootView.findViewById(R.id.ivNewsImage)).setImageURI(Uri.fromFile(tempFile));
Am I on the right lines or is there a better method?
Thanks for reading, help is appreciated!
If you are saying that you have a byte[] that contains a PNG, use BitmapFactory and its decodeByteArray() method to convert that into a Bitmap. Then, call setImageBitmap() on the ImageView with that Bitmap.
Do not write things to disk that do not need to be written to disk.
Simple Use this :
byte[] data;
public static void setImageViewWithByteArray(ImageView view, byte[] data) {
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
view.setImageBitmap(bitmap);
}

Reading an image as byte array with Android: skImageDecoder::Factory returned null

I have a Spring MVC Web Service that returns an image as a byte array. The output is in json format. The format is png
Here's the code snippet for the image.
BufferedImage img = ImageIO.read(new File(caminho.replace("/", "//")));
imagem = ((DataBufferByte) img.getRaster().getDataBuffer
()).getData();
When I run the server, this is the output:
[{"id":0,"caminhoMDPI":null,"caminhoHDPI":"C:/Users/Marcos/Pictures/postos/drawable-
mdpi/esso_logo.png","caminhoXHDPI":null,"caminhoXXHDPI":null,"imagem":"AAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAA....]
Eventually, other symbols appear on the "imagem" field. I suppose this is right, but I'm not
sure.
On my Android app, I have a routine to download the image and store it as a blob in the database. I receive the json format and transform it into a class with jackson. I've logged the "imagem" field and it looks the same.
My problem is that I can't transform it into an image. Here's the code snippet:
byte[] img_bandeira = cursor.getBlob(cursor.getColumnIndex("img_bandeira"));
Bitmap bmpBandeira = BitmapFactory.decodeByteArray(img_bandeira, 0, img_bandeira.length);
ImageView ivBandeira = (ImageView) view.findViewById(R.id.ivBandeira);
ivBandeira.setImageBitmap(bmpBandeira);
All I get is a message: skImageDecoder::Factory returned null.
I've looked at other similar posts, tried to change some lines, but nothing happened. I don't know what's going on here. Thanks in advance.
I solved it. In the server, the image should be sent like this:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedImage img = ImageIO.read(new File(caminho.replace("/", "//")));
ImageIO.write(img, "png", baos);
baos.flush();
String base64String = Base64.encode(baos.toByteArray());
baos.close();
In my app, it should be read like this:
public static Bitmap getImage(byte[] imageArray) {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(imageArray);
Bitmap bitmap = BitmapFactory.decodeStream(byteArrayInputStream);
return bitmap;
}
Thank you, everybody ;)

String to image

I use following code for get bitmap from string but i get null bitmap. So please guide me.
byte[] Image_getByte = Base64.decode(img);
ByteArrayInputStream bytes = new ByteArrayInputStream(Image_getByte);
BitmapDrawable bmd = new BitmapDrawable(bytes);
Bitmap bitmap=bmd.getBitmap();
Log.v("log","Home bitmap "+bitmap);
i.setImageBitmap(bitmap);
use BitmapFactory.decodeByteArray(Image_getByte)
here you don't need any String, after getting the byte array, just pass it in the mentioned method which returns Bitmap
The javadoc for getBitmap() says:
"Returns the bitmap used by this drawable to render. May be null."
If img is base 64 String use the following source to get the bitmap from base 64 string.
byte[] source=img.getBytes();
byte[] Image_getByte = Base64.decode(source);
Bitmap bitmap = BitmapFactory.decodeByteArray(Image_getByte, 0,Image_getByte.length);
imageView.setImageBitmap(bitmap);
This may help you
Please consider 'ImageContents' as String which contains image data.
byte[] imageAsBytes = Base64.decode(ImageContents.getBytes());
ImageView image = (ImageView)this.findViewById(R.id.ImageView);
image.setImageBitmap(
BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
);
ImageView :In Android, you can use “android.widget.ImageView” class to
display an image file
BitmapFactory:Creates Bitmap objects from various sources, including
files, streams, and byte-arrays.
For more information about BitmapFactory see here

Create ImageView from data, java android

I have data that represents an image and I want to create an ImageView
Exemple of image data :
zCH5BAAAAAAALAAAAADIABQAAAT/8MlJq7046827/2AojmRpnmiqrmzrvnAsz1ji3ARK3AMGOIbUgUYsjhA3RwFVuA0tO8ezpBj0jNgshpF0MGZN7wk5 ....
How can I do that programmatically ?
thxs
Either use API level 8 or copy the base 64 class, which you can find here:
http://developer.android.com/reference/android/util/Base64.html
base64String is a String representing the picture in a base64 format. ImageView is your imageview which you want the picture to be shown in. The String you posted earlier.
Then you just can use this snippet:
byte[] buf = Base64.decode(base64String,0);
BitmapFactory factory = new BitmapFactory();
Bitmap bitmap = factory.decodeByteArray(buf, 0, buf.length);
ImageView.setImageBitmap(bitmap);

Categories

Resources