ImageCompressor is a non-visible MIT App Inventor extension written in Java that enables on-device image compression in JPEG, PNG, and WebP formats.
ImageCompressor is a non-visible MIT App Inventor extension written in Java that enables on-device image compression in JPEG, PNG, and WebP formats. Designed for advanced developers, it provides high-performance, background-threaded processing, URI-to-path conversion, automatic naming, and event-driven callbacks to integrate seamlessly into complex App Inventor projects.
- Multi-format Support: Compress to JPEG, PNG, WebP (lossy).
- Adjustable Quality: Set compression level from 0 (max compression) to 100 (best quality).
- Automatic File Management: Creates a
CompressedImagesfolder in external storage, resolves name collisions by appending incremental suffixes. - URI-to-File Conversion: Accepts both content URIs and direct file paths.
- Event Callbacks:
ImageSelected,ImageSaved,ErrorOccurredevents for responsive, block-driven workflows. - Background Processing: Runs compression in a separate thread to maintain UI responsiveness.
- Optional Logging: Enable detailed Logcat output via
LogEnabled(true).
- Download AIX: Copy
com.bosonshiggs.imagecompressor.aixfrom theout/directory. - Add to App Inventor: In your App Inventor project, import the extension file (
.aix). - Set Permissions: Ensure
WRITE_EXTERNAL_STORAGEandREAD_EXTERNAL_STORAGEpermissions are granted in the packaged APK.
// Initialize and enable logging
ImageCompressor1.LogEnabled(true)
// Trigger image picker (use ActivityStarter or similar)
... when ButtonPick clicked do
call ActivityStarter1.StartActivity
end
// Handle selection
when ActivityStarter1.AfterActivity do
call ImageCompressor1.PathFromUri(ActivityStarter1.ResultUri)
end
// Compress the chosen image
when ImageCompressor1.ImageSelected(path) do
call ImageCompressor1.CompressImage(
path,
"compressed_photo",
75,
1001
)
end
// Receive compressed file path
when ImageCompressor1.ImageSaved(id, savedPath) do
// id = 1001, savedPath = "/storage/.../CompressedImages/compressed_photo.jpg"
call Notifier1.ShowAlert("Saved to: " & savedPath)
end
// Handle errors
when ImageCompressor1.ErrorOccurred(errorMsg) do
call Notifier1.ShowAlert("Compression error: " & errorMsg)
end
If you extend or inspect the .java source, the primary entry points are:
// Enable or disable debug logging
imageCompressor.LogEnabled(true);
// Convert URI to file path
String path = imageCompressor.PathFromUri(uriString);
// Compress on background thread
imageCompressor.CompressImage(
"/storage/.../photo.png",
"outputName",
90,
42 // developer-defined ID
);
// Implement callbacks in ActivityResultListener
@Override
public void resultReturned(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
String path = imageCompressor.PathFromUri(uri.toString());
imageCompressor.ImageSelected(path);
}
}
// Events dispatched back to blocks
public void ImageSelected(String imagePath) { /* ... */ }
public void ImageSaved(int id, String filePath) { /* ... */ }
public void ErrorOccurred(String errorMessage) { /* ... */ }- Directory Name: Modify
myDirNameconstant inImageCompressor.java. - Supported Formats: Add support for other
Bitmap.CompressFormattypes as needed. - Threading: Replace
new Thread(...)with your ownExecutorServicefor advanced concurrency control.
Clone the repository and use Fast CLI to build:
git clone git@github.com:iagolirapasssos/ImageCompressor.git
cd ImageCompressor/image-compressor
fast build -oArtifacts:
out/com.bosonshiggs.imagecompressor.aix— extension file.docs/— generated Javadoc and usage markdown.
- Fork the repository.
- Create a feature branch:
git checkout -b feature/my-extension-update. - Commit your changes with tests.
- Build and verify with
fast build -o. - Open a Pull Request describing your enhancements.
This project is licensed under the MIT License. See LICENSE for details.
For more information, visit the App Inventor community.