package uk.co.metricrat.imagenotifier; import android.app.Activity; import android.app.AlertDialog; import android.graphics.*; import android.util.*; import android.view.*; import android.widget.*; import android.widget.LinearLayout; import com.google.appinventor.components.runtime.*; import com.google.appinventor.components.annotations.SimpleEvent; import com.google.appinventor.components.annotations.SimpleFunction; import com.google.appinventor.components.runtime.AndroidNonvisibleComponent; import com.google.appinventor.components.runtime.ComponentContainer; public class ImageNotifier extends AndroidNonvisibleComponent { private final Activity activity; public ImageNotifier(ComponentContainer container) { super(container.$form()); this.activity = this.form.$context(); } @SimpleFunction(description = "Shows a provided image and an OK button, with title and caption. Use maxSize to increase or decrease image size. A value " + "of 600 works well for square, portrait and landscape images. An absolute path is required to all images on the device.") public void ShowImageNotifier(final String title, final String imagePath, final String caption, final int maxSize) { final ImageView imageView = new ImageView(this.activity); BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 8; Bitmap bitmapPicture = BitmapFactory.decodeFile(imagePath); Bitmap resizedBitmap = getResizedBitmap(bitmapPicture,maxSize); imageView.setImageBitmap(resizedBitmap); imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE); AlertDialog.Builder alert = new AlertDialog.Builder(activity); alert.setTitle(title); LinearLayout layout = new LinearLayout(activity); layout.setOrientation(LinearLayout.VERTICAL); layout.addView(imageView); final TextView capText = new TextView(this.activity); capText.setText(caption); capText.setGravity(Gravity.CENTER); layout.addView(capText); alert.setView(layout); alert.setPositiveButton("OK", (dialog, whichButton) -> AfterImageNotifier()); alert.setCancelable(false); alert.show(); } @SimpleFunction(description = "Shows a provided base64 string as an image, an OK button, a title and a caption. Use maxSize to increase or decrease " + "image size. A value of 600 works well for square, portrait and landscape images.") public void ShowImageNotifierFromBase64String(final String title, final String base64String, final String caption, final int maxSize) { final ImageView imageView = new ImageView(this.activity); BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 8; byte[] decodedString = Base64.decode(base64String, Base64.DEFAULT); Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); Bitmap resizedBitmap = getResizedBitmap(decodedByte,maxSize); imageView.setImageBitmap(resizedBitmap); imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE); AlertDialog.Builder alert = new AlertDialog.Builder(activity); alert.setTitle(title); LinearLayout layout = new LinearLayout(activity); layout.setOrientation(LinearLayout.VERTICAL); layout.addView(imageView); final TextView capText = new TextView(this.activity); capText.setText(caption); capText.setGravity(Gravity.CENTER); layout.addView(capText); alert.setView(layout); alert.setPositiveButton("OK", (dialog, whichButton) -> AfterBase64ImageNotifier()); alert.setCancelable(false); alert.show(); } //* For resize bitmap with width and height ratio. public static Bitmap getResizedBitmap(Bitmap image, int maxSize) { int width = image.getWidth(); int height = image.getHeight(); float bitmapRatio = (float) width / (float) height; if (bitmapRatio > 1) { width = maxSize; height = (int) (width / bitmapRatio); } else { height = maxSize; width = (int) (height * bitmapRatio); } return Bitmap.createScaledBitmap(image, width, height, true); } // Event raised after the user has responded to imageNotifier. @SimpleEvent(description = "Event raised after the user has responded to ImageNotifier.") public void AfterImageNotifier() { EventDispatcher.dispatchEvent(this, "AfterImageNotifier"); } // Event raised after the user has responded to base64ImageNotifier. @SimpleEvent(description = "Event raised after the user has responded to base64ImageNotifier.") public void AfterBase64ImageNotifier() { EventDispatcher.dispatchEvent(this, "AfterBase64ImageNotifier"); } }