Android bitmap The background turns black when saving

Source: Internet
Author: User

previously written an Android program, has been a bug unresolved: The background color was originally white, can save the picture when it turned black. Yesterday I took it out and looked at it. When you create a new bitmap object, the default variable should be similar to another new variable or object in Java, with a default value of 0. So suppose that when you create a new bitmap, the value of each pixel is 0, which is black. So create a bitmap that each pixel is 255 (white) on the line.

Some of the code is below.

How to get a bitmap:

1/**2* Get bitmap with corresponding background color3*@paramThe width of a width bitmap4*@paramHeight of high bitmap5*@paramThe background color of a color bitmap6*@returnThe bitmap of the color7*/8Public Bitmap Getbitmapbycolor (int width,int height,IntColor) {9Bitmap Newbitmap;10new int[ Width*height]; // Create a new pixel array, the number of elements is the width of the bitmap multiplied by the height 11 for (int i=0;i< Colors.length;i++12 Colors[i]=color;// assign a color to each pixel 13 }< Span style= "color: #008080;" >14 newbitmap= CreateBitmap (colors,width,height,bitmap.config.argb_8888); 15 return Newbitmap;                 
View Code

Construction Method:

1PublicDrawview (context context, AttributeSet AttributeSet) {2super 3 int width = context.getresources (). Getdisplaymetrics (). Widthpixels;// Get the width of the screen 4 int height = Context.getresources (). Getdisplaymetrics (). Heightpixels; // Get the height of the screen 5 Bitmap Bitmap=getbitmapbycolor (Width,height,color.white); new canvas ();  7  Canvas.setbitmap (bitmap);                 
View Code

Ways to save pictures:

1/**2* Save the picture in the Pictures folder of the memory card3*@paramFileName File name4*@throwsIOException5*/6Publicvoid Savepic (String fileName)throws IOException { 7 File File = new file ("/sdcard/pictures/" + FileName + ". png" );  File.createnewfile ();  9 FileOutputStream Fileos = new FileOutputStream (file); 10 cachebitmap.compress (Bitmap.CompressFormat.PNG, Fileos); // note is in PNG format. If set to JPG format, the background color will darken 11  Fileos.flush (); 12  Fileos.close (); < span>                
View Code

change to this, after the operation found an error! Debug breakpoints, find "canvas.setbitmap (bitmap);" Something went wrong. Check on the Internet, the original canvas object in the execution of the SetBitmap method, the first to determine whether the bitmap is mutable, if it is immutable, then you can not execute the SetBitmap () method. Bitmap.ismutable () returns a Boolean value. Returns True if the bitmap is mutable, or false instead. Embed this statement in your code, and Logcat shows that bitmap is really immutable. So we have to find a way to change the bitmap object to variable. On the Internet, there is only one way to do this is to call the copy () method of bitmap, copy one copy to another bitmap object, copy () has a parameter, you can set the copy is not a variable. However, the original bitmap object is useless.

So the other method is the same, the construction method is changed to:

1PublicDrawview (context context, AttributeSet AttributeSet) {2Super(context, AttributeSet);3int width = context.getresources (). Getdisplaymetrics (). Widthpixels;// Get the width of the screen 4 int height = context.getresources (). Getdisplaymetrics (). Heightpixels; // Get the height of the screen 5 Bitmap Tempbitmap=getbitmapbycolor (Width,height,color.white); new canvas ();  7 Bitmap bitmap=tempbitmap.copy (Tempbitmap.getconfig (), true); true indicates that the bitmap object is mutable; false instead 8  Canvas.setbitmap (bitmap); 9}             
View Code

This will run successfully.

In fact, there is another way to do this is to first execute the CreateBitmap () method and create a bitmap object. Then set all the pixel points of this bitmap object to the desired color. It has been tested that this will not cause bitmap to become immutable.

Some of the code is below.

To set the bitmap background color:

1/**2* Set the background color of the bitmap3*@paramBitmap the bitmap to be set4*@paramColor Background color5*/ 6 public void Setbitmapbgcolor (Bitmap bitmap, color) { 7 for (int i=0;i<bitmap.getwidth (); I++for (int j=0;j< Bitmap.getheight (); J++) { 9 Bitmap.setpixel (I,j,color ); // set each pixel of bitmap to the corresponding color }11 }12}                 
View Code

Construction Method:

1PublicDrawview (context context, AttributeSet AttributeSet) {2Super3 width = context.getresources (). Getdisplaymetrics (). Widthpixels;// Gets the width of the screen 4 height = context.getresources (). Getdisplaymetrics (). Heightpixels; // Get the height of the screen 5 Bitmap Bitmap=createbitmap (width,height,bitmap.config.argb_8888);  Setbitmapbgcolor (bitmap,color.white); new canvas ();  8  Canvas.setbitmap (bitmap);                
View Code

the method of saving the picture does not change. All of the above construction methods just give part of the code.

Android bitmap The background turns black when saving

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.