javafx Imageview load an image but not others - javafx

I'm using ImageView ( javafx ), I tried to load an image (jpg), it works, and when i try to load another it is not (I tried gif, png, also another jpg). I also tried to change the name of the image loaded before, and same, it doesn't load.
It just work with the first image, with same name.
I declared this :
#FXML
private ImageView imageView;
AND this is the code that worked:
InputStream is = this.mainApp.getClass().getResourceAsStream("..\\resources\\images\\facture\\recuBank\\2015\\22-12-2015\\12080351_10206938666998884_3823913475123618229_o.jpg");
if(is != null)
{
Image img = new Image(is);
this.imageView.setImage(img);
}
AND this doens't worked :
// I renamed the first image
InputStream is = this.mainApp.getClass().getResourceAsStream("..\\resources\\images\\facture\\recuBank\\2015\\22-12-2015\\a.jpg");
if(is != null)
{
Image img = new Image(is);
this.imageView.setImage(img);
}
Hope someone can help

WOW I solved it just by doing a REFRESH in folder "resources", in my project (in Eclise).
I was adding the images directly in the folder, so in my project, Eclise is not aware that any change happened.

Related

Examination of the case where ImageView contains Image or not

I'm working with JavaFX. I need to know how to check contains of ImageView.
E.g. If I have image inside ImageView I should recive information "true" if not "false", and the most important things - If this ImageView is empty how I can create 'if' conditions ?
You can check, if the image property is null. Now you may also want to consider a Image to be empty where the image could not successfully be loaded to be empty, which could be done using the following method:
public static boolean isEmpty(ImageView imageView) {
Image image = imageView.getImage();
return image == null || image.isError();
}
A thing you may also want to check is the width and height of the image.

Be careful your GUI work(click or drag to photo) in javaFX when using transparent PNG file

(English expression is very hard to me. Please correct my writing if anyone fine strange expression.)
Be careful, if you have a mind to use PNG file when make draggable or click action.
Sometimes transparent PNG will disturb dragging and click action.
So, if you want to use PNG on your dragging or click action work, use opaque PNG.
Last week, I tried to make a draggable imageview on tilepane.
It looks like photo tile puzzle.
I applied clicking and dragging action.
Then it sometimes worked and sometimes didn't workd.
I wondered why it is.
So I tried test over and over again.
Finally, I could fine a clue.
One transparent PNG has a opaque part and transparent part.
Thus, the task works when click opaque part and doesn't work when click transparent part. Lets see details below my answer.
Both result is different. One thing point to imageview, and another thing point to hbox(parent of imageview).
1.When click opaque part of PNG
#FXML
private void tile1MouseClicked(MouseEvent event) {
System.out.println("result : " + event.getTarget());
}
result : ImageView#7afa16c0
2.When click transparent part of PNG
#FXML
private void tile1MouseClicked(MouseEvent event) {
System.out.println("result : " + event.getTarget());
}
result : HBox#3f30efe6
String[] imageName = {"slide1.png", "slide2.png", "slide3.png", "slide4.png",
"slide5.png", "slide6.png", "slide7.png", "slide8.png"};
Image img = null;
for (int i = 0; i < imageName.length; i++) {
try {
img = new Image(getClass().getResourceAsStream(imageName[i]));
} catch (Exception e) {
System.out.println("exception : " + e);
}
ImageView imageview = new ImageView(img);
imageview.setFitWidth(20);
imageview.setFitHeight(20);
HBox hbox = new HBox();
hbox.getChildren().add(imageview);
tile1.getChildren().add(hbox);
}

RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap

I have a card view which has a custom adapter and it loads image into its target using Picasso(inside onBindViewHolder(lazy load) ).
The custom row has a button on which when clicked opens fragment which displays image in wide view , Using the bitmap from original imageview
( before in fragment i check if bitmap is loaded fully in main imageview using a boolean in callback of Picasso to 'true' in onSucess() . till then my button is disabled ) like :
BindViewHolder:
Picasso.with(context)
.load(cl.getUrlPhoto())
.resize(500, 500)
.error(R.drawable.images)
.into(cardViewHolder.urlPhoto, new Callback() {
#Override
public void onSuccess() {
cardViewHolder.imgL=true;
cardViewHolder.fab.setEnabled(true);
cardViewHolder.pb_b.setVisibility(View.INVISIBLE);
}
#Override
public void onError() {
cardViewHolder.imgL=false;
cardViewHolder.fab.setEnabled(false);
cardViewHolder.pb_b.setVisibility(View.GONE);
}
});
(i check here for the boolean to be true then do this part of code )Fragment part where it loads :
imageView.buildDrawingCache(true);
Bitmap bitmap = imageView.getDrawingCache(true);
BitmapDrawable bitmapDrawable = (BitmapDrawable)
imageView.getDrawable();
bitmap1 = bitmapDrawable.getBitmap();
scaleImageView.setImageBitmap(bitmap1);
. But Sometimes on scrolling when the images are still loading or loaded or just clicking to view full image 2-3 times it crashes and gives error
" RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap . "
This exception is raised in situations where you try to recycle or set to null an image which is loaded by Picasso which means the Picasso library does not expect the image loaded by it to be recycled and when it tries to load that an exception is thrown. hope this solution works for you
Try replacing Picasso with Glide

Adobe Air Mobile + Camera - Retaining image

I have a page that displays data about an object. At the top of the page is room for an icon, showing a picture of that object. Tapping this icon brings up a new page that allows the user to take a new picture, and save it as a temporary new picture for the object (not put in database, but should persist for the session)
Initial page:
private var source:Object = new Object();
protected function onInitialize():void {
source = navigator.poppedViewReturnedObject;
}
When setting source for the image later...
if (source != null) {
pic.source = source.object;
}
else {
pic.source = "no_picture_available_image.png";
}
2nd Page (User can take picture, and view new picture):
[Bindable]
private var imageSource:Object = null;
<s:Image id="pic" width="90%" height="75%" horizontalCenter="0" source="{imageSource}" />
After taking picture...
protected function mediaPromiseLoaded(evt:Event):void {
var loaderInfo:LoaderInfo = evt.target as LoaderInfo;
imageSource = loaderInfo.loader;
}
This does show the picture just taken correctly on this page.
To get back to old page, i use navigator.popView, and use:
override public function createReturnObject():Object {
return imageSource;
}
Unfortunately, it doesn't work. The imageSource isn't null when it is read from navigator.poppedViewReturnedObject, but no image is shown.
Does the LoaderInfo not persist after popping the view? Are the camera pics not automatically saved? I can't find answers to any of these questions, and I can't debug using the phone in my current environment.
After thinking about this a bit, don't return LoaderInfo.loader as the poppedViewReturnedObject. If I remember correctly, a DisplayObject can only be set as the source of one Image. Instead, return LoaderInfo.loader.content.bitmapData. That BitmapData should be the raw data used to display the image. This data can be used repeatedly to create images and can be set as the source of an Image.
Problem turned out to be in my first page's image declaration - I didn't set a width. Seemingly, the object being displayed couldn't handle not having a specified width.
Note that passing back the loader did work fine.

Copy BitmapData From mx:Image

How can I copy or duplicate the bitmapdata from a mx:image component?
I need to display the same image in multiple screens of my application and don't want to have to download the image multiple times.
I could just use a urlrequest to download the image as a bitmap and copy that but I like the way you can can just set the source of the image component.
Image extends SWFLoader which has a content property that will contain the Bitmap object that was loaded. Wait for the image to load, cast the content to Bitmap and read its bitmapData
public function imageLoadCompleteHandler(e:Event):void
{
var bitmap:Bitmap = img.content as Bitmap;
if(bitmap == null) {
trace("loaded content is not an image");
return;
}
bmpData = bitmap.bitmapData;
//hurray..!
}
Actually after reading the message above (and trying stuff out) I think this is wrong. The Complete listener fires on my second image so I guess it is loading it twice. Oh well.
I couldn't place the whole Flex doc, but this seemed to work. I added this to the Application tag: applicationComplete="Run();"
The following is wrapped in a mx:Script tag:
import mx.controls.Image;
private function Run():void
{
var i:Image = new Image();
i.source = first.source;
addChild(i);
}
And this is just a Image outside of the script tag:
<mx:Image x="12" y="125" source="e53c04a51d5992fb77ab1e20c45ddc9f.jpeg" id="first" />
I also tried this after setting the i source:
first.source = null;
Just to see what happens, the i image keeps it's source

Resources