π Please help us complete a 5 minute survey!
Published on March 24, 2022
π·οΈ Tags: #extensions
A non-visible extension to turn components to images.
Made with Niotron IDE at https://ide.niotron.com.
Current version: 1 Package name: com.gordonlu.viewutil.aix
For more information, read this MIT App Inventor Community thread.
π Documentation
Event blocks
Failed - This event is fired when the extension has encountered an error. Possible reasons: quality is not a number between 0 and 100, wrong path, etc.
Parameters: error = text
SavedScreenshot - This event is fired when the extension has saved the image at the path.
Parameters: path = text
Method blocks
SaveComponentAsImage - Saves the component as an image with the given path, compress format and the given quality. The quality parameter shall be a value between 0 and 100, and the path parameter shall be an absolute path.
The compressFormat parameter only accepts "JPEG" or "PNG". If not, then the file format should be "JPEG". The path should be something like: storage/emulated/0/Android/data/appinventor.ai_gordonlu0749.Save/filesimage.jpg.
Parameters: component = component, path = text, compressFormat = text, quality = number (int)
Property blocks
Jpeg - A compress format block. Returns: "JPEG"
Png - A compress format block. Returns: "PNG"
Open Source
package com.gordonlu.viewutil;
import android.app.Activity;
import android.content.Context;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;
import com.google.appinventor.components.runtime.AndroidViewComponent;
import android.view.View;
import android.graphics.Bitmap;
import java.io.FileOutputStream;
import android.graphics.Bitmap.CompressFormat;
@DesignerComponent(
version = 1,
description = "A non-visible extension to save a component as an image.",
category = ComponentCategory.EXTENSION,
nonVisible = true,
iconName = "https://docs.google.com/drawings/d/e/2PACX-1vQCI87PHLBF0jb8QWyYmIRQSjjNW3EFXf-qpsWCvBYkUQ9vEgPAB8SpxcMpblxNpbIYrjCjLrRLIU2c/pub?w=16&h=16")
@SimpleObject(external = true)
//Libraries
@UsesLibraries(libraries = "")
//Permissions
@UsesPermissions(permissionNames = "")
public class ViewUtil extends AndroidNonvisibleComponent {
//Activity and Context
private Context context;
private Activity activity;
public ViewUtil(ComponentContainer container){
super(container.$form());
this.activity = container.$context();
this.context = container.$context();
}
@SimpleFunction(description = "Saves the component as an image with the given path, compress format and the given quality.")
public void SaveComponentAsImage(AndroidViewComponent component, String path, String compressFormat, int quality) {
// https://stackoverflow.com/a/3527902/17802442
try {
View view = component.getView();
view.setDrawingCacheEnabled(true);
Bitmap b = view.getDrawingCache();
CompressFormat cp = CompressFormat.JPEG;
if (compressFormat == "PNG") {
cp = CompressFormat.PNG;
}
b.compress(cp, quality, new FileOutputStream(path));
Saved(path);
}
catch(Throwable e) {
Failed(e.getMessage());
}
}
@SimpleProperty(description = "A compress format block.")
public String Jpeg() {
return "JPEG";
}
@SimpleProperty(description = "A compress format block.")
public String Png() {
return "PNG";
}
@SimpleEvent(description = "This event is fired when the extension has failed to save the image.")
public void Failed (String error) {
EventDispatcher.dispatchEvent(this, "Failed", error);
}
@SimpleEvent(description = "This event is fired when the extension has saved the image.")
public void Saved (String path) {
EventDispatcher.dispatchEvent(this, "Saved", path);
}
}
Downloads
βοΈ Tests
All of these tests are successful in saving the image in storage/emulated/0/Download and the ASD (application-specific directory).
π οΈ Companion:
Android 11 API 30, Xiaomi 11 5G NE Lite.
π οΈ APK:
Android 8.1 API 27, Google Pixel XL emulator.
Android 9 API 28, Google Pixel 5 emulator.
Android 11 API 30, Xiaomi 11 5G NE Lite.
Android 11 API 30, Google Pixel 2 emulator.
π οΈ Requirements:
In Screen1 properties, set DefaultFileScope to Legacy.
You must have the write external storage permission (a.k.a. Files/photos and media).
For details on how to read external storage, please search the forum.
TERMS AND CONDITIONS
By downloading or sharing my extension, you agree to follow these terms and conditions.
Have fun using the extension!
This, along with the MIT App Inventor original post above, are the only authorized places to download this extension. As an organization, you are not allowed to host and advertize this extension on your website, document, tweet, blog, article or any software you made without my authorization. You are also reminded not to include a direct download link of this extension on your website without my permission. If you are interested in the actions listed above, please contact me via email. Do remember that if you violate term no. 2, AICODE has the right to flag your website, document, tweet, blog or article as a violation of copyright, or other legal actions will be taken. Please contact me if you find anyone do this.
However, you can copy the link of this webpage and share it to someone as a personal identity (not as an organization), or sharing it on these authorized forums: App Inventor, Kodular, Niotron, Appzard and Android Builder communities. The term organization includes schools, markets and app stores, companies and social groups. Thank you for your understanding.
Those organizations are authorized to advertize or host my extension:
Kodular admins
MIT App Inventor admins
Pura Vida Apps / Taifun Baer
Selling this extension/product to anyone is not allowed unless authorized by AICODE.
These terms and conditions are written and issued on December 20, 2021. AICODE has the total permission to edit these terms and conditions anytime.
Β© Gordon Lu π§ E-mail π¬ Message π Phone call