With some help from @Juan_Antonio I believe we have found the issue. He had a go at building the extension the AI2 way, and this generated an error on this line
Thanks all for help and support, especially @Juan_Antonio
JAVA
package uk.co.metricrat.imagenotifier;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.*;
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) {
Bitmap bitmapPicture = BitmapFactory.decodeFile(imagePath);
final ImageView imageView = new ImageView(this.activity);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
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", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
AfterImageNotifier();
}
});
alert.setCancelable(false);
alert.show();
}
// 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");
}
@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", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
AfterBase64ImageNotifier();
}
});
alert.setCancelable(false);
alert.show();
}
// 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");
}
//* 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);
}
}
uk.co.metricrat.imagenotifierV4.aix
I'll update first post in due course. (edit, link now updated)
App Inventor has never allowed for lambda functions in components/extensions. In the ant build.xml for components we specify Java 7 as the source/target versions and lambdas were add in Java 8. You should use an anonymous inner class instead.
My IDE, IntelliJ Idea, suggests the notifier.java way as a problem, and to convert
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
AfterImageNotifier();
}
});