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 image in android logicchip 1

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.

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.




Scanning the gallery

As a matter of fact creating an image does’t mean it’s visible in gallery. Therefore we need to refresh the media scanner and update it with new images path.

 

Full code MainActivity

Xml file for main activity

 

 

Leave a Reply

%d bloggers like this: