#imageview cannot find the image file

1 messages · Page 1 of 1 (latest)

tired pendant
#

Hi, im working with a game project and I need to set the guy's avatar in a small imageview, the dimensions apart, the image doesn't even load..

shut pivotBOT
#

<@&987246487241105418> please have a look, thanks.

shut pivotBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

tired pendant
#

if someone answers I will send the code

fringe viper
tired pendant
#

anyways

#

so basically

#

I put all my data in a json file

#

like this:

if (file.exists()) {
                String imagePath = getClass().getResource(profileData.getAvatar()).toExternalForm();
                Image image = new Image(imagePath);
                System.out.println("Image loaded from: " + imagePath);
                if (image != null) {
                    picc.setImage(image);
                } else {
                    System.out.println("Image could not be loaded.");
                }
            }
fringe viper
#

image can't be null

tired pendant
#

this is the json

#
{
      "nickname": "de",
      "avatar": "C:\\Users\\Andrew\\Pictures\\Roblox\\RobloxScreenShot20230701_234606681.png",
      "partiteGiocate": 0,
      "partiteVinte": 0,
      "partitePerse": 0,
      "livello": 0,
      "password": "dee"
    },
shut pivotBOT
tired pendant
#

so

fringe viper
#

if you know, why do you test if it is null ?

tired pendant
#

the issue might be this "\\"

tired pendant
#

just for debugging

fringe viper
#

it cannot be null

tired pendant
#

why

fringe viper
#

because new keyword will never return null

tired pendant
#

ah

#

alr, imma change that soon

#

but

#

the issue lays afterward

#

lemme show u

#

I mean before

#

because, once the image is set

#

it will be like this:

if (selectedFile != null) {
            Image selectedImage = new Image(selectedFile.toURI().toString());

            // Get the name of the selected file
            String filePath = selectedFile.getPath().toString();
            filePath = filePath.replace("/", "\\");
            System.out.println("Selected file name: " + filePath);
            Avatar.setText(filePath);
            profiloplayer.getInstance().getProfilo().setAvatar(filePath);
            profiloplayer.getInstance().getProfileManager().saveToJson();
        } else {
            Avatar.setText("No Image Selected");
        }
shut pivotBOT
tired pendant
tired pendant
#

meaning that the file path is wrong

fringe viper
#

also

#

getClass().getResource(profileData.getAvatar()).toExternalForm()
This won't work if you use a jar

tired pendant
#

which jar?

fringe viper
#

filePath = filePath.replace("/", "\");
This is system dependent, please don't do that

fringe viper
tired pendant
#

its fine, it has to be .zip

#

but

tired pendant
fringe viper
fringe viper
tired pendant
#

ok so

#

the user selects an image

fringe viper
tired pendant
#

I get the absolute path

tired pendant
fringe viper
fringe viper
tired pendant
tired pendant
#

it does build

fringe viper
#

yes

tired pendant
#

but it could be since its a jar

#

I mean its a .java

fringe viper
#

there are two ways to build your program, either into a folder, or into a jar, if it's into a jar, your approach breaks

tired pendant
#

oh then its fine, its within a folder

#

anyways

#

lemme show u the program a bit deeply

fringe viper
#

I am not talking about the .java files

#

I am talking about how your compile it

fringe viper
tired pendant
#

about the file?

fringe viper
#

yes

#

show an example

tired pendant
fringe viper
#

it can't work

#

you can only use things that are in resources folder with Class.getResource

tired pendant
#

ah..

#

isnt there a way to get files outside

fringe viper
#

it wouldn't make sense otherwise

fringe viper
tired pendant
#

I messed up

#

whats the other way?

fringe viper
#

io

#

but please answer me

#

why ?

#

why are you files not in resources ?

tired pendant
#

because the user might want to get his image somewhere

fringe viper
#

but the user won't fill this json right ?

tired pendant
#

no

fringe viper
#

so how do you get the path of the user ?

#

from a gui ?

tired pendant
#

its kinda like when u send an image from discord

#

I made a little window where you can select yoru files

fringe viper
#

right

tired pendant
#

aka the file chooser class

fringe viper
#

then yes, using getResource here doesn't make sense

#

getResource is if you want to get programresources, things that you build and ship with your program

tired pendant
#

so in order to set the image into an imageview while having the absolutepath I have to use Io?

fringe viper
#

here you want to use io

tired pendant
#

so I just create a file with the path and give it to the Image object?

fringe viper
tired pendant
#

javafx.scene.image.Image;

fringe viper
tired pendant
#

oh

#

lemme see

#

it works letss go, thks man

fringe viper
#

@tired pendant also how did you coded the gui to ask the user an image to use ?

tired pendant
fringe viper
tired pendant
#

because the gui is about your profile

#

so you can insert

#

all the data

#

while the rest is entered by the game

fringe viper
#

how did you coded the gui to ask the user an image to use ?

tired pendant
#

ahh I misred

tired pendant
fringe viper
#

ah right thank you

tired pendant
#

np