Load file from Sample File Directory named "images" - directory

I have this simple structure
Now I should load 11.png to ImageView from URI.
How do I do this?
imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageURI(Uri.fromFile(new File("What should be here???")));

I think you Are New To Android?
Its Better To move the Image File to Drawable Folder and and Set Image View From the Drawable. You Can Use BatchDrawableImport Plugin in Android Studio to Import multiple Drawable Files
imageView = (ImageView)findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.yourImageName);
Or Move the File to Assets Folder and Using Assets Manager You Can Achieve the Solution
AssetManager manager = getAssets();
// Read a Bitmap from Assets
try {
InputStream open = manager.open("icon.png");
Bitmap bitmap = BitmapFactory.decodeStream(open);
ImageView view = (ImageView) findViewById(R.id.ImageView01);
view.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}

Related

save a number of images in one png file

I have a number of images that are displayed in my application. They are generated by the data I enter. Now I want to save the images in one png file (would prefer jpg but png is also doing the job). Right now the last image will be overwritten by the next image, so that only the last image in stored in the png file. I thought about creating one image that consists of all the images I want to save in the png file. Does anyone have an idea of how to do that? Or is there a simpler way of how to save the images in one file?
private final ObservableList<Image> pdfFilePages = FXCollections.observableArrayList();
pngButton.setOnAction(e -> {
if (pdfFilePages.size() > 0) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle(I18N.get("key.save_to_png"));
File file = fileChooser.showSaveDialog(dialogStage);
if (file != null) {
for (Image img : pdfFilePages) {
ImageView imgView = new ImageView(img);
try {
BufferedImage bi = SwingFXUtils.fromFXImage(img, null);
ImageIO.write(bi, "png", file);
} catch (IOException e1) {
LOGGER.error(e1.getMessage(), e1);
}
}
}
}
});

FXML/JavaFX How to make an ImageView Clickable?

I want to change the Image in the Image View using FXML and to change the Image I open the fileChooser to pick an image that should replace the old one , I have 2 issues right now :
How to make the ImageView Clickable this is the code I'm using for this
public void imagePicker(){
Defaultview.setPickOnBounds(true); // allows click on transparent areas
Defaultview.setOnMouseClicked((MouseEvent e) -> {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Resource File");
fileChooser.showOpenDialog(new Stage());
});
}
in my FXML file I have:
<ImageView fx:id="Defaultview" fitHeight="93" fitWidth="93"
pickOnBounds="true" preserveRatio="true" onAction='#imagePicker'>
and for the image change I'd like to change it from
Image url="#../images/default.png"
to the Image Chosen .
ImageView doesn't have an onAction property (so I think you should get a runtime error when you load your FXML file). If you want to respond to mouse clicks, then you should use onMouseClicked:
<ImageView fx:id="Defaultview" fitHeight="93" fitWidth="93"
pickOnBounds="true" preserveRatio="true" onMouseClicked='#imagePicker'>
Registering the event handler in FXML means that the imagePicker() method will be invoked when the event occurs (i.e. when the user clicks on the image view). There is no need (and it's incorrect) to set the onClicked handler from inside the method that is invoked when the onClick event occurs. All you need is
public void imagePicker(){
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Resource File");
// use existing window here, don't create a new one:
File file = fileChooser.showOpenDialog(Defaultview.getScene().getWindow());
if (file != null) {
Defaultview.setImage(new Image(file.toURI().toString()));
}
}
In JavaFx ImageView has setOnMouseClicked property,so if you want to import an image from your PC using fileChooser ,you need to add an extension to your FileChooser to define only images and skip other files
public void imagePicker(){
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilterJPG = new FileChooser.ExtensionFilter("JPG files (*.jpg)", "*.JPG");
FileChooser.ExtensionFilter extFilterPNG = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.PNG");
fileChooser.getExtensionFilters().addAll(extFilterJPG, extFilterPNG);
File ChooserFile = fileChooser.showOpenDialog(Defaultview.getScene().getWindow());//your owner
if (ChooserFile != null) {
Image image = new Image(ChooserFile.toURI().toString());
Defaultview.setImage(image);
}
}
and add MouseEvent (setOnMouseClicked) to your ImageView :
Defaultview.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
imagePicker();
}
});

how to load image name from database and load it into my imageview

i have a project in which i have in my db images stored with their names only so i want to load those image with javafx
and i have no idea how to load thos images
public void initialize(URL url, ResourceBundle rb) {
// help me here to retreive my images names
}
and this is my button that allows me to change my cover picture
here is the code
changecover.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
try {
FileChooser fileChooser = new FileChooser();
//Set extension filter
FileChooser.ExtensionFilter extFilterJPG = new FileChooser.ExtensionFilter("JPG files (*.jpg)", "*.JPG");
FileChooser.ExtensionFilter extFilterPNG = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.PNG");
fileChooser.getExtensionFilters().addAll(extFilterJPG, extFilterPNG);
//Show open file dialog
File file = fileChooser.showOpenDialog(null);
BufferedImage bufferedImage = ImageIO.read(file);
Image image = SwingFXUtils.toFXImage(bufferedImage, null);
coverpic.setImage(image);
Groupe g = gs.getGroupe(5);
g.setPhoto_couverture(coverpic.toString());
gs.update(g);
//myImageView.setImage(image);
} catch (IOException ex) {
Logger.getLogger(Groupe_homeController.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
Why load the image data to a BufferedImage and then convert this BufferedImage to a JavaFX Image when you can avoid storing the image data in memory twice by directly loading the data to a Image:
Image image = new Image(file.toURI().toURL().toExternalForm());
I'm not sure how a database is involved here though, since FileChooser allows you to choose the image from storage devices available to the client only, which means you'd need to implement some way of retrieving the data from the server... (unless the database is for use on the local machine only)

how open image with url in javafx

I have banner an I wont to put it in my javaFX application.
And when user click on the image open default browser.
try {
String path = "http://developer.am/webservice/banner728x90.gif";
URL url = new URL(path);
BufferedImage image = ImageIO.read(url);
label = new JLabel(new ImageIcon(image));
} catch (Exception exp) {
exp.printStackTrace();
}
also I am trying to convert above code from awt in JavaFX
Lets see. First the ingredients:
Image
Button
ImageView
Open Link in System Browser with JavaFX
Putting this together:
String path = "http://...";
String pathToOpen = "http://...";
Image image = new Image(path);
ImageView imageView = new ImageView(image);
Button button = new Button("clickMe!", imageView);
button.setOnAction(ev -> getHostServices().showDocument(pathToOpen));

Print Stage with JavaFX 8

I found some basic example how to print web page with JavaFX but I'm looking for examplewhich can print the complete Scene with app content. Is there any example?
You can convert your scene to an image using
WritableImage snapshot = scene.snapshot(null);
This will return a WritableImage, which can be converted to an Image File or BufferedImage, and print using the Printing API of JavaFX8 (there are not lot of examples available for this, but the new API has quite a resemblance to the old Printing API, so it won't be a problem)
Converting WritableImage to Png File
WritableImage snapshot = scene.snapshot(null);
File file = new File("image.png");
try {
ImageIO.write(SwingFXUtils.fromFXImage(snapshot, null), "png", file);
} catch (IOException e) {
e.printStackTrace();
}
Converting WritableImage to BufferedImage (used for printing)
WritableImage snapshot = scene.snapshot(null);
BufferedImage bufferedImage = SwingFXUtils.fromFXImage(snapshot, null);
For just a small example of how to print an Image using java, please go through
Proper way of printing a BufferedImage in Java
How to print image in java

Resources