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

bitmap android
copy bitmap c#
resize bitmap android
bitmap compress
android compress bitmap without losing quality programmatically
import bitmap android
android bitmap density
bitmap properties 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).

  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.

Bitmap createBitmap, Creates a new bitmap, scaled from an existing bitmap, when possible. specified, and then copying this bitmap's pixels into the new bitmap. To create copy of bitmap you can use: Bitmap newBmp = Bitmap.createScaledBitmap(src, dstWidth, dstHeight, filter); You can gradually get the Image from SD card. NO problem with this implementation. If you are using Hashmap then you can user the image URL as the Key for Hashmap. Depending on situation you can use:

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

Saving & loading bitmaps to the Android device storage (internal , 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. The core idea here was to take an existing bitmap, and replace part of that bitmap with another (smaller?) bitmap, and this is what this article describes. What you do with the resulting bitmap is limited only by your imagination. The following is a description of the function in the demo app that does all of the related work.

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.

How to: create a new copy of an existing Bitmap? - android, createScaledBitmap(qrBitmap, targetSize, targetSize, false); originBitmap = originBitmap.copy(Bitmap.Config.ARGB_8888, true); Canvas canvas = new  I am trying to copy bmp1 (clean image) to bmp2(changed image) when I click "Clear" button. But I don't want to use createBitmap() or copy() function. Because these are create new Bitmap objects. I want to use only my two bitmaps (bmp1 and bmp2). How can I copy bmp1 to bmp2? I search google but everbody do this with createBitmap() or copy(). Thanks.

Bitmap OLDBitmap = getBitmap();
Bitmap newBmp = Bitmap.createBitmap(OLDBitmap);

android.graphics.Bitmap.copy java code examples, We are testing a new feature in our Android application — a fancy photo though, because an image we decode into an existing Bitmap must be the for reuse and creates a new one if there is no Bitmap to serve a request. There are many ways of creating or obtaining a bitmap. We have already seen how a bitmap file, .jpg, gif or .png can be included in the drawable/ resource directory and displayed in an ImageView control. In many cases a bitmap can be acquired by loading a file, reading in a collection of bytes or even taking a photo with the Android camera.

you will get the copy of bitmap as bitmap2

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

Reusing Bitmap objects on Android, Developing Android 9 Apps Using Android Studio 3.2, Java and Android Jetpack Neil When adding this feature to an application, the first step is to create a new the form of either a Bitmap object or a Uri reference to the image): Bitmap bitmap creating HTML content (as opposed to loading and printing an existing web  Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata, 0, bitmapdata.length); In my case, bitmapdata only has the buffer of the pixels, so it is imposible for the function decodeByteArray to guess which the width, the height and the color bits use.

Android Studio 3.2 Development Essentials, If the file does not exist, the remote file is retrieved and made into a Bitmap in lines 14–18. To do that, you create an OutputStream for writing data and handle all file open and close functionality. There is no need to find an existing record. Click the target app module in the Project window (while in either the Android or Project view), and then select File > New > Android resource file. Fill in the details in the dialog: File name: Type the name for the XML file (does not require the .xml suffix). Resource type: Select the type of resource you want to create.

Android Application Development in 24 Hours, Sams Teach Yourself, The following code example demonstrates how to construct a new Bitmap Copy. Bitmap image1; private void Button1_Click(System.Object sender, System. Initializes a new instance of the Bitmap class from the specified existing image. So, we have to save the decoded bytes into some where and clear existing bitmap, then create a new mutable bitmap and load back the saved bytes into bitmap again. Even to copy bytes we cannot create another ByteBuffer inside the memory. In that case need to use MappedByteBuffer that will allocate bytes inside a disk file.

Bitmap Class (System.Drawing), GVRBitmapTexture(GVRContext gvrContext, android.graphics.Bitmap bitmap). Constructs a texture Constructs a texture using a pre-existing Bitmap and the user defined filters GVRTextureParameters . Create a new, grayscale texture, from an array of luminance bytes. Copy new luminance data to a grayscale texture. It is not necessary to create a new bitmap to draw on it. You can also draw on an existing bitmap. The Monkey Moustache page uses its constructor to load the MonkeyFace.png image. It then creates an SKCanvas object based on that bitmap, and uses SKPaint and SKPath objects to draw a moustache on it:

Comments
  • >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?
  • 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 ...
  • 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.
  • @Sujit the createScaledBitmap will return the orignal Bitmap obj if the scaling is 1 and no filtering is done. just be aware of that.
  • 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.
  • 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.
  • 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'
  • Returns an immutable bitmap from the source bitmap.
  • 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?
  • 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.