56

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).

eros
  • 4,946
  • 18
  • 53
  • 78

7 Answers7

198

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);
Community
  • 1
  • 1
MSquare
  • 6,311
  • 4
  • 31
  • 37
  • 30
    Actually this is the **only** correct answer to the question on how to create a `Bitmap` copy. Both, `createScaledBitmap` and `createBitmap` are allowed to return the original instance, according to the documentation. This should be the accepted answer. – theV0ID Jun 03 '14 at 13:59
6

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.

Adil Aliyev
  • 1,159
  • 2
  • 9
  • 22
  • 1
    Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others. – lucascaro Nov 09 '18 at 06:42
3
Bitmap OLDBitmap = getBitmap();
Bitmap newBmp = Bitmap.createBitmap(OLDBitmap);
jeet.chanchawat
  • 5,842
  • 5
  • 38
  • 59
  • 12
    Actually, it doesn't work always/on all devices. From documentation: 'The new bitmap may be the same object as source, or a copy may have been made' – sandrstar Feb 22 '14 at 09:05
  • Returns an immutable bitmap from the source bitmap. – jeet.chanchawat Oct 27 '15 at 11:08
  • The API document says that "The new bitmap may be the same object as source, or a copy may have been made". But how does it use the same object as source and how how copy a new object? – Aaron Lee Oct 16 '17 at 10:57
1
public static Bitmap cloneBitmap(Bitmap bitmap) {
    return bitmap.copy(bitmap.getConfig(),bitmap.isMutable());
}
Gil SH
  • 3,789
  • 1
  • 27
  • 25
  • 1
    Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others. – lucascaro Nov 09 '18 at 06:41
1

Kotlin extension:

fun Bitmap.copy(): Bitmap? = copy(config, isMutable)
Renetik
  • 5,887
  • 1
  • 47
  • 66
0

you will get the copy of bitmap as bitmap2

Bitmap bitmap2= bitmap1.copy(bitmap1.getConfig(), true);

dileep krishnan
  • 326
  • 4
  • 7
-8
  1. To create copy of bitmap you can use:

    Bitmap newBmp = Bitmap.createScaledBitmap(src, dstWidth, dstHeight, filter);

  2. You can gradually get the Image from SD card. NO problem with this implementation.

  3. If you are using Hashmap then you can user the image URL as the Key for Hashmap.

Community
  • 1
  • 1
Sujit
  • 10,512
  • 9
  • 40
  • 45
  • >image url as map key? -> how do i determine the floor map if i don't have reference to the floor number? that's why, I am planning to use floor number as a map key. May i ask your opinion? – eros Sep 08 '11 at 05:29
  • are you retrieving the floor image dynamically from server or it is static and always be in the SDCard ? If it is static images then you can use floor number of the image or file name as the key ... – Sujit Sep 08 '11 at 05:52
  • i will be downloaded from uurl using background service then store it on the fixed place on the sdcard. in other words, it is already available on the sdcard. – eros Sep 08 '11 at 07:32
  • 6
    @Sujit the createScaledBitmap will return the orignal Bitmap obj if the scaling is 1 and no filtering is done. just be aware of that. – VJ Vélan Solutions Oct 16 '12 at 19:46