use of com.codename1.ui.Image in project CodenameOne by codenameone.
the class AndroidImplementation method openGallery.
public void openGallery(final ActionListener response, int type) {
if (getActivity() == null) {
throw new RuntimeException("Cannot open galery in background mode");
}
if (!checkForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, "This is required to browse the photos")) {
return;
}
callback = new EventDispatcher();
callback.addListener(response);
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
if (type == Display.GALLERY_VIDEO) {
galleryIntent.setType("video/*");
} else if (type == Display.GALLERY_IMAGE) {
galleryIntent.setType("image/*");
} else if (type == -9999) {
galleryIntent = new Intent();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
galleryIntent.setAction(Intent.ACTION_OPEN_DOCUMENT);
} else {
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
}
galleryIntent.addCategory(Intent.CATEGORY_OPENABLE);
// set MIME type for image
galleryIntent.setType("*/*");
galleryIntent.putExtra(Intent.EXTRA_MIME_TYPES, Display.getInstance().getProperty("android.openGallery.accept", "*/*").split(","));
} else {
galleryIntent.setType("*/*");
}
this.getActivity().startActivityForResult(galleryIntent, OPEN_GALLERY);
}
use of com.codename1.ui.Image in project CodenameOne by codenameone.
the class CodenameOneActivity method onPrepareOptionsMenu.
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.clear();
try {
Form currentForm = Display.getInstance().getCurrent();
if (currentForm == null) {
return false;
}
int numCommands = currentForm.getCommandCount();
// If there are no commands, there's nothing to put in the menu
if (numCommands == 0) {
return false;
}
// Build menu items from commands
for (int n = 0; n < numCommands; n++) {
Command command = currentForm.getCommand(n);
if (command != null) {
String txt = currentForm.getUIManager().localize(command.getCommandName(), command.getCommandName());
MenuItem item = menu.add(Menu.NONE, n, Menu.NONE, txt);
Image icon = command.getIcon();
if (icon != null) {
Bitmap b = (Bitmap) icon.getImage();
// Using BitmapDrawable with resources, to use device density (from 1.6 and above).
BitmapDrawable d = new BitmapDrawable(getResources(), b);
item.setIcon(d);
}
if (!command.isEnabled()) {
item.setEnabled(false);
}
if (android.os.Build.VERSION.SDK_INT >= 11 && command.getClientProperty("android:showAsAction") != null) {
String androidShowAsAction = command.getClientProperty("android:showAsAction").toString();
// "ifRoom" | "never" | "withText" | "always" | "collapseActionView"
if (androidShowAsAction.equalsIgnoreCase("ifRoom")) {
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
} else if (androidShowAsAction.equalsIgnoreCase("never")) {
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
} else if (androidShowAsAction.equalsIgnoreCase("withText")) {
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
} else if (androidShowAsAction.equalsIgnoreCase("always")) {
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
} else if (android.os.Build.VERSION.SDK_INT >= 14 && androidShowAsAction.equalsIgnoreCase("collapseActionView")) {
// MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
item.setShowAsAction(8);
}
}
}
}
} catch (Throwable t) {
}
return nativeMenu;
}
use of com.codename1.ui.Image in project CodenameOne by codenameone.
the class LocalNotificationPublisher method createAndroidNotification.
private Notification createAndroidNotification(Context context, LocalNotification localNotif, PendingIntent content) {
Context ctx = context;
int smallIcon = ctx.getResources().getIdentifier("ic_stat_notify", "drawable", ctx.getApplicationInfo().packageName);
int icon = ctx.getResources().getIdentifier("icon", "drawable", ctx.getApplicationInfo().packageName);
if (smallIcon == 0) {
smallIcon = icon;
} else {
icon = smallIcon;
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
builder.setContentTitle(localNotif.getAlertTitle());
builder.setContentText(localNotif.getAlertBody());
builder.setAutoCancel(true);
if (localNotif.getBadgeNumber() > 0) {
builder.setNumber(localNotif.getBadgeNumber());
}
String image = localNotif.getAlertImage();
if (image != null && image.length() > 0) {
if (image.startsWith("/")) {
image = image.substring(1);
}
InputStream in;
try {
in = context.getAssets().open(image);
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap im = BitmapFactory.decodeStream(in, null, opts);
builder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(im));
} catch (IOException ex) {
Logger.getLogger(LocalNotificationPublisher.class.getName()).log(Level.SEVERE, null, ex);
}
}
builder.setSmallIcon(smallIcon);
builder.setContentIntent(content);
String sound = localNotif.getAlertSound();
if (sound != null && sound.length() > 0) {
sound = sound.toLowerCase();
builder.setSound(android.net.Uri.parse("android.resource://" + ctx.getApplicationInfo().packageName + "/raw" + sound.substring(0, sound.indexOf("."))));
}
Notification n = builder.build();
n.icon = icon;
if (sound == null || sound.length() == 0) {
n.defaults |= Notification.DEFAULT_SOUND;
}
return n;
}
use of com.codename1.ui.Image in project CodenameOne by codenameone.
the class Capture method capturePhoto.
/**
* <p>Invokes the camera and takes a photo synchronously while blocking the EDT, the sample below
* demonstrates a simple usage and applying a mask to the result</p>
* <script src="https://gist.github.com/codenameone/b18c37dfcc7de752e0e6.js"></script>
* <img src="https://www.codenameone.com/img/developer-guide/graphics-image-masking.png" alt="Picture after the capture was complete and the resulted image was rounded. The background was set to red so the rounding effect will be more noticeable" />
*
* @param width the target width for the image if possible, some platforms don't support scaling. To maintain aspect ratio set to -1
* @param height the target height for the image if possible, some platforms don't support scaling. To maintain aspect ratio set to -1
* @return the photo file location or null if the user canceled
*/
public static String capturePhoto(int width, int height) {
CallBack c = new CallBack();
if ("ios".equals(Display.getInstance().getPlatformName()) && (width != -1 || height != -1)) {
// workaround for threading issues in iOS https://github.com/codenameone/CodenameOne/issues/2246
capturePhoto(c);
Display.getInstance().invokeAndBlock(c);
if (c.url == null) {
return null;
}
ImageIO scale = Display.getInstance().getImageIO();
if (scale != null) {
try {
String path = c.url.substring(0, c.url.indexOf(".")) + "s" + c.url.substring(c.url.indexOf("."));
OutputStream os = FileSystemStorage.getInstance().openOutputStream(path);
scale.save(c.url, os, ImageIO.FORMAT_JPEG, width, height, 1);
Util.cleanup(os);
FileSystemStorage.getInstance().delete(c.url);
return path;
} catch (IOException ex) {
Log.e(ex);
}
}
} else {
c.targetWidth = width;
c.targetHeight = height;
capturePhoto(c);
Display.getInstance().invokeAndBlock(c);
}
return c.url;
}
use of com.codename1.ui.Image in project CodenameOne by codenameone.
the class FileEncodedImage method create.
/**
* Creates an encoded image that maps to a local file thus allowing to
* seamlessly fetch files as needed. This only works reasonably well for very small
* files. This version of the method creates the file from an input stream
*
* @param fileName the name of the file
* @param i input stream from which to create the file
* @param width the width of the file or -1 if unknown (notice that this will improve performance)
* @param height the height of the file or -1 if unknown (notice that this will improve performance)
* @return image that will load the file seamlessly
*/
public static FileEncodedImage create(String fileName, InputStream i, int width, int height) throws IOException {
EncodedImage e = EncodedImage.create(i);
FileEncodedImage f = new FileEncodedImage(fileName, width, height, true);
f.data = e.getImageData();
OutputStream o = FileSystemStorage.getInstance().openOutputStream(fileName);
o.write(f.data);
o.close();
return f;
}
Aggregations