Creating Image From Current View in Android

Creating Image From Current View in Android

Sometimes we are require to create images in our application,This project is all about creating image from view.In addition a view can be Relative-layout, Linear-layout etc. However for creating image from view in android first convert the view to bitmap format. Then the converted bitmap file is saved as a readable image format. Finally scan the gallery for making the newly created images visible.

Before everything add permission’s for reading from and writing to the external storage in manifest file.

Creating bitmap from view

In order to create a bitmap first define a bitmap with the same size as the view with bitmap configuration ARGB_8888. In fact a bitmap configuration describes how pixels are stored. This affects the quality (color depth) as well as the ability to display transparent/translucent colors. Accordingly for bitmap configuration “ARGB_8888” each pixel is stored on 4 bytes. Another part is creating a canvas and bind to the bitmap, Canvas is where the images are drawn.

//does not have background drawable, then draw white background on the canvas

canvas.drawColor(Color.WHITE);

}

// draw the view on the canvas

view.draw(canvas);

//return the bitmap

returnreturnedBitmap;

}

Saving the bitmap

In the first place create a folder to save the images inside the picture directory or any other place. In any case check if it’s there else create it. Next is the image name it can’t be same because it overwrite the previous image, For this reason we are using system time in milliseconds for the name. As a result not two names are same. Then an image file is created with system time as the name. Also a bitmap object is created and it have the bitmap result of the view to bitmap conversion. Although image file is created it’s blank and FileOutputStreamis used for writing bitmap data to the image file.