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.
Related
Is it necessary to set a Bitmap to null after recycling it, so that its memory can be freed?
In the following code:
private static Bitmap bmpConcatHV() {
Bitmap bmp = BitmapFactory.decodeResource(resId);
Bitmap bmp2 = concatVertically(bmp, bmp);
bmp.recycle();
bmp = null;
Bitmap bmp3 = concatHorizontally(bmp2, bmp2);
bmp2.recycle();
bmp2 = null;
return bmp3;
}
There are Lint warnings:
The value null assigned to 'bmp' is never used
The value null assigned to 'bmp2' is never used
Both bmp and bmp2 are local variables, so no.
BitmapFactory.decodeResource(resId);
bitmap has two part of information when loaded into memory
1- information about bitmap ==> exists in java used memory
2- information about pixels of bitmap (byte of array) ==>exists in c++ used memory
Bitmap.recycle() is used to free the memory of C++.
Garbage Collection will collection the part of java and the memory
Garbage Collection work when need memory but i need to use this reference now so will use
bmp = null; here i handle the part of java and be safe to make it null
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.
I need to convert an int array into a Bitmap. Here is what I've tried (assume that myBmp is an array of ints that represent pixel values).
Bitmap myBmp = Bitmap.createBitmap(outBmp, 100, 100, Bitmap.Config.ARGB_8888);
ImageView imgView = (ImageView)findViewById(R.id.imgView);
imgView.setImageBitmap(myBmp);
What is supposed to happen is that I see the image being displayed on screen (after the array becomes that image). However, all I see is an empty screen.
Is there an alternative way to take an int[] array and convert it into a Bitmap, or am I just doing something wrong?
If I create an object and assign it to a variable:
Obj obj1 = null;
obj1 = myFunction(params);
(here myFunction creates a complex object)
And later I reassign the variable:
obj1 = myFunction(otherparams);
Does in that moment a memory leak occur, because I did not destroy the previous object?
Here is the real situation:
Bitmap bmp;
bmp = drawMyBitmap(3);
//... some code
bmp = drawMyBitmap(4);
Will a memory leak happen here?
Of cource, I know that I must call bmp.recycle, but I can't do it, because the real code is the following:
Bitmap bmp;
bmp = drawMyBitmap(3);
imageView.setImageBitmap(bmp);
//... some code
// if I try to do recycle here - I receive java.lang.IllegalArgumentException: Cannot draw recycled bitmaps
// But I need to recreate bitmap every some minutes
bmp = drawMyBitmap(4);
imageView.setImageBitmap(bmp);
So, how can I recycle the bitmap and avoid memory leaks?
As I understand, your problem is just you can't recycle your Bitmap cause it's used.
It's pretty naive, so maybe it's wrong, but do this:
imageView.setImageBitmap(bmp);
//... some code
Bitmap tmp = bmp;
bmp = drawMyBitmap(4);
imageView.setImageBitmap(bmp);
tmp.recycle(); // As it's not anymore referenced by the ImageView, you can recycle the Bitmap safely
I didn't test it. Give feedback.
In the first case, you will release the first object's reference so the garbage collector will destroy it, leaving the second one live on memory because of new reference.
In the second case, if you are setting bitmaps to ImageViews, you can not recycle them because the view will not have the bitmap to draw the image and it will throw to you a bitmap recycled exception, so you are not "leaking" to much memory keeping 2 bitmaps on memory.
Try to use bitmap options to create them to optimise your memory comsuption if you want.
Bitmap bmp;
bmp = drawMyBitmap(3);
imageView.setImageBitmap(bmp);
//... some code
// if I try to do recycle here - I receive java.lang.IllegalArgumentException: Cannot draw recycled bitmaps
// But I need to recreate bitmap every some minutes
Bitmap temp = bmp; //try this
bmp = drawMyBitmap(4);
imageView.setImageBitmap(bmp);
temp.recycle();
I am loading a bitmap from a resource like so:
Bitmap mBackground = BitmapFactory.decodeResource(res,R.drawable.image);
What I want to do is make some changes to the bitmap before It gets drawn to the main canvas in my draw method (As it would seem wasteful to repeat lots of drawing in my main loop when it isn't going to change). I am making the changes to the bitmap with the following:
Canvas c = new Canvas(mBackground);
c.drawARGB(...); // etc
So naturally I get an exception
java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
So to avoid that I made a copy of the bitmap so that it is mutable
Bitmap mBackground = BitmapFactory.decodeResource(res,R.drawable.image).copy(Bitmap.Config.ARGB_8888, true);
Which avoid the problem however it sometimes causes OutOfMemoryExceptions, do know any better ways of achieving what I want?
Use decodeResource(Resources res, int id, BitmapFactory.Options opts) and specify inMutable in the options.
http://developer.android.com/reference/android/graphics/BitmapFactory.html
You'd better use RapidDecoder.
import rapid.decoder.BitmapDecoder;
Bitmap mBackground = BitmapDecoder.from(res, R.drawable.image)
.mutable().decode();
Works for API level 8.
Instad of yours:
Bitmap mBackground = BitmapFactory.decodeResource(res,R.drawable.image);
Use:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
Bitmap mBackground = BitmapFactory.decodeResource(res,R.drawable.image, options);