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);
Related
I am working on creating an image super-resolution application that uses a TensorFlow Lite model. The model gives the output Image in the form of ByteBuffer and I convert the ByteBuffer to Bitmap. Next, I display this Bitmap but nothing shows up. The code I am using can be seen below:
ByteBuffer out = ByteBuffer.allocate(4 * 384 * 384 * 3);
tflite.run(byteBuffer,out);
byte[] imageBytes= new byte[out.remaining()];
out.get(imageBytes);
final Bitmap outPut_Image = BitmapFactory.decodeByteArray(imageBytes,0,imageBytes.length);
//Toast.makeText(this,tflite.toString(),Toast.LENGTH_LONG).show();
Toast.makeText(this, "Working",Toast.LENGTH_LONG).show();
ImageView imageView = (ImageView) this.findViewById(R.id.imageView2);
imageView.setImageBitmap(outPut_Image);
Please advise me what I am doing wrong here.
BitmapFactory.decodeByteArray() is used for compressed image data such as JPEG.
For raw RGB888 data, I think you should convert it to ARGB_8888 format first.
After that you can use the following snippet to create Bitmap object.
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(buffer);
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.
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);
}
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
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.