save any view as image in sketchware

How to save any view as an image from your sketchware project?

hi there today in this tutorial, we will see how to save any view from your sketchware project.

it will be like taking a screenshot from your app. We will setup thing so that. When someone clicks a button or an image (you can chose it as you wish). The screen will be captured as an image file and will be sved to the gallery.

Follow this steps to get started.

Step no.1 : Create the variables and more block

first create a string variable called image_path

and then create a more block called view_share

remember while creating the more block. Add a view called view and a string variable called image_path. This is very important. in Case you can’t understand then the below image will help you.

Step no.2 : Define your more block

inside your more block add a add sorce directly block and paste the below code

Bitmap returnedBitmap = Bitmap.createBitmap(_view.getWidth(), _view.getHeight(),Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(returnedBitmap);
android.graphics.drawable.Drawable bgDrawable =_view.getBackground();
if (bgDrawable!=null) {
bgDrawable.draw(canvas);
} else {
canvas.drawColor(Color.TRANSPARENT);
}
_view.draw(canvas);

java.io.File pictureFile = new java.io.File(image_path);
if (pictureFile == null) {
showMessage("Error creating media file, check storage permissions: ");
return; }
try {
java.io.FileOutputStream fos = new java.io.FileOutputStream(pictureFile); returnedBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
showMessage("Image Saved in " + image_path + " folder");
} catch (java.io.FileNotFoundException e) {
showMessage("File not found: " + e.getMessage()); } catch (java.io.IOException e) {
showMessage("Error accessing file: " + e.getMessage());

}

Step no.3 : Setup the onCreate event

In your on create event, drag and drop a make directory path block and a join block and get external storage directory . Join the get external storage directory block and the text /img/img. png

It will be the location, where your images will be saved. The name of the image file will be img.png you can modify the location as well as the image name as per your choice.

define the string variable image_path as below

Step no.4 define when and what to save.

For me, I want to save the whole thing inside the linear1 when I click the imageview1.

Go to imageview1 on clicked event (you can set your own).

Drag and drop the more block which we created earlier. Chose view as linear1 (again you can shose a different view. Select the view which you want to save) and drop the image_path variable in the place of path.

and you are all done.

This is how you can save any view inside the linear1 when as an image in your sketchware project.

Hope this tutorial helps. please keep sharing this tutorial if you found this helpfull

you can also check out this tutorial

  1. Double tap to exit
  2. Add a share button in your app

Leave a Reply