How to create an image from a View and save it in Sketchware
Using codes in add source directly block in Sketchware, we can create bitmap images from any View (like EditText, TextView, LinearLayout, WebView, etc.) in android app. The bitmap images can then be saved to externalcache or to external storage if the permissions to WRITE EXTERNAL STORAGE is added.
To Create an app which can create images from any View and share it, follow the steps given below.
1. Start a new Sketchware project. In VIEW area add an LinearLayout (linear1) containing things to be converted to an image, and a Button (button1) for converting linear1 to image.. If you want to preview the image add an ImageView also.
2. In LOGIC area of project, create a More Block saveView[View:view].
Here use an add source directly block and add following 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.WHITE);
}
_view.draw(canvas);
java.io.File pictureFile = new java.io.File(Environment.getExternalStorageDirectory() + "/Download/myimage.png");
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 /Download/ folder");
} catch (java.io.FileNotFoundException e) {
showMessage("File not found: " + e.getMessage()); } catch (java.io.IOException e) {
showMessage("Error accessing file: " + e.getMessage());
}
This code defines a class saveView(View), which converts a View to Bitmap image and saves it to Download folder in External storage directory.
3. Now in event onButtonClick, use blocks as shown below.
This will add WRITE EXTERNAL STORAGE permissions to the app, and save linear1 in as image in /Download/ folder.
4. Save and run the project. Now when you click button1, it will save linear1 as image in external storage.
To Create an app which can create images from any View and share it, follow the steps given below.
1. Start a new Sketchware project. In VIEW area add an LinearLayout (linear1) containing things to be converted to an image, and a Button (button1) for converting linear1 to image.. If you want to preview the image add an ImageView also.
2. In LOGIC area of project, create a More Block saveView[View:view].
Here use an add source directly block and add following 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.WHITE);
}
_view.draw(canvas);
java.io.File pictureFile = new java.io.File(Environment.getExternalStorageDirectory() + "/Download/myimage.png");
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 /Download/ folder");
} catch (java.io.FileNotFoundException e) {
showMessage("File not found: " + e.getMessage()); } catch (java.io.IOException e) {
showMessage("Error accessing file: " + e.getMessage());
}
This code defines a class saveView(View), which converts a View to Bitmap image and saves it to Download folder in External storage directory.
3. Now in event onButtonClick, use blocks as shown below.
This will add WRITE EXTERNAL STORAGE permissions to the app, and save linear1 in as image in /Download/ folder.
4. Save and run the project. Now when you click button1, it will save linear1 as image in external storage.
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHi, thank you for the code that you provided above, it works like a dream. I now have the option to share the view over any application that allows this.
ReplyDeleteI do have one question though. By pressing button, how can i save this image on another page in my app for future use?
Good question I'm also looking for that answer
DeletestoreImage saves the image in External cache. Thus it can be used in any page by using it's Uri.
DeleteExample:
Uri uri = Uri.parse(getExternalCacheDir() + "/image.jpg");
imageview1.setImageURI(uri);
(Request) how to make screenshot button)
ReplyDeleteHow can i only save it to gallery??
ReplyDeleteI'm also looking for that answer
DeletePlease answer it
DeleteI'm also looking for that . Plz help
DeleteIs there a way where we can edit the file name? And change the file directory after it's been put into the cache file
ReplyDeletehey the bitmap image quality is too bad. can we make an high graphics image with this ? any help.
ReplyDeleteHow to make it work on image from image manager then export to a specific directory?
ReplyDeleteHi,
ReplyDeleteHow can rename the new file , if the file name is already exist.
This comment has been removed by the author.
ReplyDeleteIs there a way to save the source of an ImageView to file as well?
ReplyDeletePublish your apps for free
ReplyDeleteSigmaapp.blogspot.com
Don't be late and visit
Also view my apps
Write permission denied. How can I add the r/w ext. storage permission to the manifest?
ReplyDeleteHi, can this code work on PDF file on webview and how to open or load PDF file and save it. Thanks
ReplyDeletePls where do I see the downloaded image??
ReplyDeleteThis comment has been removed by the author.
ReplyDelete