I have a LinearLayout with a default Background, but in some circumstances I need to set a custom Background from a byte[] data from Blob object in SQL Database.
I alreay have made the custom Adapter but I have no idea how do the image part.
if (Ad.getAd_image().length > 0) {
//Ad.getAd_image() is a byte[] object
ad_image_layout.setBackground(???);
}
We can achieve by 2 ways,
1. If you have direct byte[] you can use
byte[] b = //your data;
Drawable image = new BitmapDrawable(getResources(), BitmapFactory.decodeByteArray(b, 0, b.length));
If you have input stream you can use this
InputStream is = //your input stream;
Bitmap bitmap = BitmapFactory.decodeStream(is);
do this
Bitmap bitmap=BitmapFactory.decodeByteArray(Ad.getAd_image(),0,Ad.getAd_image().length);
Drawable d = new BitmapDrawable(getResources(), bitmap);
LinearLayout l;
l=(LinearLayout)findViewById(R.id.layout_id)
l.setBackground(d);
Check out the decodeByteArray methods of BitmapFactory. The byte array must be in one of the supported image formats.
Related
When you have an image (PNG in this case) saved as byte array on android, how can scale it to get a new image scaled in byte array format?
Have in mind that the image to scaling is to a smaller size, avoiding loss of data.
Image croping byte uri image convert byte array the step
InputStream iStream = getContentResolver().openInputStream(uri);
byte[] inputData = getBytes(iStream);
One quick way of doing this, it's letting Android to resolve the algorithm for us.
So, we convert the byte array to a bitmap, the bitmap can create a new bitmap with new sizes defined and finally convert back to byte array.
private byte[] getScaledImage(byte[] originalImage, int newWidth, int newHeight) {
// PNG has not losses, it just ignores this field when compressing
final int COMPRESS_QUALITY = 0;
// Get the bitmap from byte array since, the bitmap has the the resize function
Bitmap bitmapImage = (BitmapFactory.decodeByteArray(originalImage, 0, originalImage.length));
// New bitmap with the correct size, may not return a null object
Bitmap mutableBitmapImage = Bitmap.createScaledBitmap(bitmapImage,newWidth, newHeight, false);
// Get the byte array from tbe bitmap to be returned
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
mutableBitmapImage.compress(Bitmap.CompressFormat.PNG, 0 , outputStream);
if (mutableBitmapImage != bitmapImage) {
mutableBitmapImage.recycle();
} // else they are the same, just recycle once
bitmapImage.recycle();
return outputStream.toByteArray();
}
I have an image which I turn into a byte array. I then encrypt this byte array with AES and I want a visual result to display representing this encryption.
The problem is all the header and meta information is also encrypted so this encrypted byte array, which is passed to byteToImage(), is not recognized as a valid representation of an image i.e. decodeByteArray() returns null.
I have tried cutting off the first 512 bytes of the original image and appending that back on to the start of the encrypted byte array in the hope that this will restore the header information - but it hasn't worked. I have this with .png and .bmp images. What I ideally want is a way to represent a RAW image in android and encrypt this information byte for byte - leaving out the need to fiddle around with headers etc.
I would really appreciate any help.
private static byte[] imageToBytes(ImageView iv){
byte[] imageInByte = null;
Bitmap originalImage;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BitmapDrawable drawable = (BitmapDrawable) iv.getDrawable();
originalImage = drawable.getBitmap();
originalImage.compress(Bitmap.CompressFormat.PNG, 0, baos); // was 70
imageInByte = baos.toByteArray();
return imageInByte;
}
private static Bitmap bytesToImage(byte data[]) {
// byte[] x = Base64.decode(data, Base64.DEFAULT); using x in place of data also fails
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
return bmp;
}
Your idea will not work. If you encrypt just a lot of bytes of a bitmap byte array you will demolish its structure and after that you don't have a bitmap anymore. So you cannot display it. If you want to be able to display the 'encrypted' image then do it pixel wise. Have a look at Bitmap.getPixel()/setPixel() or Bitmap.getPixels()/setPixels().
I have been trying to pass a single byte array (compressed bitmap) from one activity to another. When I attempt to decode the byte array back to a bitmap, and show the bitmap, it appears to be a completely transparent bitmap.
I use this code on the first activity to compress the bitmap and send the byte array:
try {
InputStream selectedImage = getContentResolver().openInputStream(Uri.parse(photoPath));
bitmap = BitmapFactory.decodeStream(selectedImage);
} catch (FileNotFoundException exception) {
Log.e(this.toString(), exception.toString());
}
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte [] byteArray = stream.toByteArray();
Log.e("PANTHERIT_BYTEARRAY", byteArray.toString());
Intent stickerActivity = new Intent (this, StickerActivity.class);
stickerActivity.putExtra("byteArray", byteArray);
startActivity(stickerActivity);
And I use this code in the accepting activity to decompress and store the bitmap:
byte [] byteArray = getIntent().getByteArrayExtra("byteArray");
Log.e("STICKER_BYTEARRAY", byteArray.toString());
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length, options);
ImageView bitmapview = (ImageView) findViewById(R.id.bitmap_view);
bitmapview.setImageBitmap(bitmap);
I copied the code to the first activity, compressed the bitmap and immediately decompressed it. I took the decompressed bitmap and set it to an ImageView in that activity and it worked fine. So the error seems to be in the passing of the byte array from the first activity to the second, but I cannot figure out why that would be.
I found an answer. I do not really know why the byte array was getting messed up, maybe it was too large? Even so, both the input and output byte arrays were the same length, so not a very good explanation.
Anyway, I found my answer here: Send Bitmap Using Intent Android. The first answer by Zaid Daghestani is the same as what I initially tried, but his edit is what worked. Instead of passing a byte array, you save the compressed bitmap to a temporary file, and pass the filename through the intent. Definitely works for passing a bitmap, now I just have to figure out how to display the bitmap on a SurfaceView instead of an ImageView...
Also, just in case you need a mutable bitmap (in order to pass it to an ImageView) you can use:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
bitmap = BitmapFactory.decodeStream(input, null, options);
I want to save an image in an ImageView to the database using R.id not R.drawable because the image can change(because its a camera picture). l can not use the path to the image to save because the gallery might be tampered around with.
HotOrNot entry = new HotOrNot(this);
byte[] image4 = HotOrNot.getBytes(BitmapFactory.decodeResource(null, R.id.imageView2));
entry.open();
entry.createEntry(image4);
entry.close();
public static byte[] getBytes(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0, stream);
return stream.toByteArray();
}
You need to understand that, R.drawable.image is the ID of the actual image. And R.id.imageView2 is the ID of the ImageView.
Your argument is that the image you want to store can change. So in that case, what you can do is use yourImageView.getDrawable(), so that you will get the current image in the ImageView and then convert it to Bitmap using this
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
Bitmap bitmap = bitmapDrawable.getBitmap();
byte[] image4 = HotOrNot.getBytes(bitmap); // Your code from here.
Whatever is represented by R.id.. is an id for a resource type from the project's pre compiled assets. I don't think you can get an id value for an image taken with the Camera.
Also note that the type of R.id.. is an int.
I know the above is not an answer. If you want to take an image using the Camera and save it in the database, check this question in SO. Hope it helps.
I wanna create an bitmap array. Is it possible?
If yes, which is the way to declare the Bitmap array. and how to initialize it?
Thank you
You could use an Arraylist :
ArrayList<Bitmap> bitmapArray = new ArrayList<Bitmap>();
bitmapArray.add(myBitMap); // Add a bitmap
bitmapArray.get(0); // Get first bitmap
or simply an Array of bitmap like :
Bitmap[] bitmapArray = new Bitmap[];
Nevertheless be careful with the size of your image. You will probably have some trouble if you try to store lot of big image.
Yes it is possible,
If bitmap1 and bitmap2 are objects of bitmap. I can assign them to an array as follows.
Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(),R.drawable.a_thumb);//assign your bitmap;
Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(),R.drawable.anotherimage);//assign your bitmap;
Bitmap[] arrayOfBitmap = {bitmap1, bitmap2};
Thanks
Deepak
Just like any array, for example:
Bitmap[] bitmaps = new Bitmap[] { BitmapFactory.decodeResource(...) /* etc. */ }
There is nothing special in the fact that the objects in array are Bitmaps.
You can make bitmap aaray in android like this,
byte[] bMapArray= new byte[buf.available()];
buf.read(bMapArray);
Bitmap bMap = BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length);
image.setImageBitmap(bMap);
Use this android developer documentation for details.
Use this too
All the above/below solutions are possible but a common case is actually using a HashMap as it is the case with Image Downloaders and async bitmap loaders.
A Bitmap is an object as may others (String arrays, Integer arrays etc...) so you might modify those methods you find also for storing bitmap arrays.