Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to: create a new copy of an existing Bitmap?

Tags:

android

I will create a simple floor map guide. I have different FLOORS and the corresponding MAPS. FLOORS are buttons and MAPS are png files stored in the sdcard. When I click 1F and corresponding 1Fmap will be displayed and so with other floors.

I am thinking of the following:

  1. one image view to show the selected map.
  2. Hashmap ( OR ) to handle the bitmaps. use to obtain the bitmap based on the selected floor. then set to ImageView via setImageBitmap(..)
  3. the bitmap to be assigned in the Hashmap are downloaded upon clicking of the floor button. then create the bitmap, set to imageview and the later on store to hashmap upon clicking the other floors.

Here are my technical/design problems:

  1. how to create a copy of bitmap?
  2. is it ok to store it to hashmap gradually or obtain it from the sdcard everytime the floor buttons are click?
  3. if i will be using hashmap, is it ok to use Integer (floor numbers) or String (floornames) as map key?

UPDATE: additional, I am targeting maximum of 20 floors (it means 20 512x512 png files...i am thinking also to adjust it to 256x256 as others suggested).

like image 874
eros Avatar asked Sep 08 '11 05:09

eros


People also ask

What is recycle in bitmap?

The recycle() method allows an app to reclaim memory as soon as possible. Caution: You should use recycle() only when you are sure that the bitmap is no longer being used. If you call recycle() and later attempt to draw the bitmap, you will get the error: "Canvas: trying to use a recycled bitmap" .

How do I know if my Android BMP is empty?

You can do a check when you want to return the BitMap look to see if the ArrayList of Paths is bigger than 0 and return the BitMap if so, or else return null.

How do I create a bitmap from resources?

To create a bitmap from a resource, you use the BitmapFactory method decodeResource(): Bitmap bitmap = BitmapFactory. decodeResource(getResources(), R. drawable.

What are bitmaps in Android?

A bitmap is simply a rectangle of pixels. Each pixel can be set to a given color but exactly what color depends on the type of the pixel. The first two parameters give the width and the height in pixels. The third parameter specifies the type of pixel you want to use.


2 Answers

This answer helped me:

https://stackoverflow.com/a/17068594/1373248

The code is the following:

Bitmap bmp1 = BitmapFactory.decodeResource(cont.getResources(), R.drawable.image); //then create a copy of bitmap bmp1 into bmp2 Bitmap bmp2 = bmp1.copy(bmp1.getConfig(), true); 
like image 110
MSquare Avatar answered Sep 17 '22 19:09

MSquare


Depending on situation you can use:

Bitmap src = ...; Bitmap dst = src.copy(src.getConfig(), src.isMutable); 

The code below creates a copy. That means it copies the pixels from source bitmap and makes completely new Bitmap object. The reason why I am pointing it out because on internet you can find many examples where they use Bitmap.createBitmap() which does not guarantee if the new bitmap will be an object or a reference to older one. And depending on situation you can have problematic behaviour.

like image 39
Adil Aliyev Avatar answered Sep 21 '22 19:09

Adil Aliyev