First, attach a section of code to save the image to the local, you can use this method or directly Debug the program, click the view button of the Bitmap to view the content of the Bitmap

public static void saveBitmap(Bitmap bitmap,String path) {
        try {
            File filePic = new File(path);
            if(! filePic.exists()) { filePic.getParentFile().mkdirs(); filePic.createNewFile(); } FileOutputStream fos =new FileOutputStream(filePic);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            return; }}Copy the code

The image was originally an Android icon, but changed to a black background after saving

Search on the Internet and some said that you can fill the background color to achieve, but in this way after adding the program has been error, if it is a new Bitmap can be directly filled with color

bitmap.eraseColor(Color.WHITE); // Fill the background color
Copy the code

The copying method below also doesn’t work

 Bitmap bitmap= outB.copy(Bitmap.Config.ARGB_8888,true);
Copy the code

Finally, IN other projects, I found that Canvas can be used to set the background to white, and then draw pictures on it, so as to achieve the effect of filling the background color. Attached is the final code:

Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(bitmap, 0.0.null);
Copy the code

The final newBitmap is an image filled with a white background, this problem is solved, hopefully to help developers in need.