在filechooser的帮助下,JavaFX在Imageview中获取图像存储路径需要帮助D:

时间:2017-01-02 19:01:59

标签: image javafx imageview fxml filechooser

我的问题是我需要帮助从fxml文件中的imageview中的图像获取路径,因为我需要将图像的路径存储在对象中。我正在使用filechooser将图像输入到imageView中。我打算将该路径存储到一个对象中,并在稍后的时间将其声明,就像个人资料页面会保留个人资料图片一样。我知道它很简短,但我希望如果你们有任何方式帮助我,我将不胜感激。

所以根据我编辑的评论: 我的问题是我无法声明图片路径的网址,尽管我使用的是文件追踪器。我实际上已删除了所有相关部分,因为我无法声明我所声明的网址。< / p>

    package orgRegistrationtest;

public class OrgRegistrationController {

@FXML
private JFXHamburger hamburger;

@FXML
private JFXTextField email;

@FXML
private JFXCheckBox health;

@FXML
private JFXCheckBox animal;

@FXML
private JFXCheckBox children;

@FXML
private JFXCheckBox elderly;

@FXML
private JFXCheckBox environment;

@FXML
private JFXCheckBox others;

@FXML
private JFXTextField name;

@FXML
private JFXTextField contact;

@FXML
private JFXTextField address;

@FXML
private JFXPasswordField password;

@FXML
private JFXPasswordField password2;

@FXML
private JFXTextArea desc;

//pic not added 

@FXML
private ImageView pic;

@FXML
private JFXButton upload;

@FXML
private JFXButton clear;

@FXML
private JFXButton confirm;

@FXML
private JFXDrawer drawer;

@FXML
void handleClear(ActionEvent event) {

}


private String type=null;



@FXML

     private void handleConfirmButtonAction(ActionEvent event) throws IOException {

        if(health.isSelected())
            type=health.getText();
        if(animal.isSelected()){
            type+=";"+animal.getText();
        }
        if(children.isSelected()){
            type+=";"+children.getText();
        }
        if(elderly.isSelected()){
            type+=";"+elderly.getText();
        }
        if(environment.isSelected()){
            type+=";"+environment.getText();
            }
        if(others.isSelected()){
            type+=";"+others.getText();
        }

        String Email=email.getText();
        String Password = password.getText();
        String Password2=password2.getText();
        String Name = name.getText();
        String Contact= contact.getText();
        String Address=address.getText();
        String Type = ""+type;
        String Desc = desc.getText();
        //String url = pic.getImage()._____
        //.getURL(); i know getURL is not inside same for getPath();
        // So how am i able to claim the Path of the Image and store it?


        OrgData table = new OrgData(Email,Password,Password2,Name, Contact,Address,Type,Desc,url);

        OrgDataDAO dao = new OrgDataDAO();
        dao.createFriend(table);




}




@FXML
public void handleUpload(ActionEvent t) {
    FileChooser fileChooser = new FileChooser();

    //Set extension filter


    //Show open file dialog
    File file = fileChooser.showOpenDialog(null);

    try {
        BufferedImage bufferedImage = ImageIO.read(file);
        Image image = SwingFXUtils.toFXImage(bufferedImage, null);
        pic.setImage(image);
    } catch (IOException ex) {
        System.out.println(ex);
    }
}


private Main Main;

public Main getMainApp() {
    return Main;
}

// setter for mainApp
public void setMainApp(Main testmainApp) {
    this.Main = testmainApp;
}

这是我拥有的模型我从来没有在URL的字符串中放入,但因为我无法将其声明并且可能有更好的方法而不是使用路径,尽管我尝试了它并且它给了我一无所获。

  public class OrgData implements Serializable {
private String email, pw, pw2, name, contact, address, type, desc,url;

final static ObservableList<OrgData> data = FXCollections.observableArrayList();


public OrgData(String email, String pw, String pw2, String name, String contact, String address, String type,
        String desc,String url) {
    super();
    this.email = email;
    this.pw = pw;
    this.pw2 = pw2;
    this.name = name;
    this.contact = contact;
    this.address = address;
    this.type = type;
    this.desc = desc;
    //this.url = url;
}

//public String getUrl() {
//  return url;
//}

//public void setUrl(String url) {
//    this.url= url;
//}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPw() {
    return pw;
}

public void setPw(String pw) {
    this.pw = pw;
}

public String getPw2() {
    return pw2;
}

public void setPw2(String pw2) {
    this.pw2 = pw2;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getContact() {
    return contact;
}

public void setContact(String contact) {
    this.contact = contact;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getType() {
    return type;
}



public String getDesc() {
    return desc;
}

public void setDesc(String desc) {
    this.desc = desc;
}



public static ObservableList<OrgData> getData() {
    return data;
}

public String toString(){
    return "["+email+","+pw+","+name+","+contact+","+address+","+type+","+desc+","+url+"]";
}

所以这是DAO,我在用户点击注册按钮并存储在OrgRegData.dat文件中后,最后保存文件详细信息。 就像我说的那样,我的问题是我无法从imagechooser中选择的imageView图像中获取路径并将其存储到我的模型中,即使是作为String也没有getPath()或getURL();点击注册后的一种方法。我没有使用数据库或任何只是使用文件和文本文件来存储数据和从文件中读取数据,所以我希望现在更清楚我的问题

    public class OrgDataDAO {
private static final String Friend_File="OrgRegData.dat";
private File dataFile;


public OrgDataDAO() {
    Path dPath = FileSystems.getDefault().getPath("resources/data/",Friend_File);
    dataFile=new File(dPath.toString());     
}

public ObservableList<OrgData> getAllFriends() {
    Scanner in;
    String record=null;
    String[] fields;
    ImageView[] images;

    ObservableList<OrgData> friends=FXCollections.observableArrayList();;
    try {
        in=new Scanner(dataFile);
        while (in.hasNextLine()) {
            record=in.nextLine();
            fields=record.split(",");
            String email=fields[0];
            String password=fields[1];
            String password2=fields[2];
            String name=fields[3];
            String contact = fields[4];
            String address=fields[5];
            String type=fields[6];
            String desc=fields[7];
            //String url = fields[8];

            OrgData f=new OrgData(email,password,password2,name,contact,address,type,desc,url);
            friends.add(f);
        }
        in.close();
    } catch (FileNotFoundException e) { 
        System.out.println("No record found!");
        //e.printStackTrace();
    }
    return friends;
}

/* assuming friend has a unique name, otherwise may return a list */
public OrgData getEvent(String email) {
    // TODO Auto-generated method stub
    ObservableList<OrgData> event=getAllFriends();
    OrgData Event=null;
    for (OrgData f:event) {
        if (f.getEmail().equals(email)){
            Event=f;
            break;
        }
    }
    return Event;
}

public void updateEvent(OrgData Event) {
    ObservableList<OrgData> Events=getAllFriends();
    for (int i=0; i<Events.size(); i++) {
        OrgData f=Events.get(i);
        if (f.getEmail().equals(Event.getEmail())){
            Events.set(i, Event);
        }
    }
    synToFile(Events);
}

public void deleteEvent(OrgData Event) {

    ObservableList<OrgData> Events=getAllFriends();

    OrgData delEvent=null;
    for (OrgData f:Events) {
        if (Event.getEmail().equals(f.getEmail())){
            delEvent=f;         
            break;
        }
    }
    if (delEvent!=null){
        Events.remove(delEvent);
        synToFile(Events);      
    }       

}

public boolean createFriend(OrgData Event) {
    boolean existing=false;
    ObservableList<OrgData> Events=getAllFriends();
    for (OrgData f:Events) {
        if (f.getEmail().equals(Event.getEmail())){
            existing=true;
            break;
        }
    }
    if (!existing) {
        Events.add(Event);
        synToFile(Events);
    }
    return !existing;
}

private void synToFile(ObservableList<OrgData> EventList) {
    if (EventList==null)
        return;

    try {
        FileWriter out = new FileWriter(dataFile);
        for (OrgData f: EventList) {
            out.append(f.toString()+"\n");
        }
        out.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

2 个答案:

答案 0 :(得分:2)

您需要重新考虑将图像存储为Image对象。

Image对象不包含任何有关路径的信息(因为您已经发现),因为您可以从任何InputStream加载它并且可能没有办法使用InputStream访问URL。 (例如,图像可以作为blob存储在数据库中,进行检索所需的查询...)

打开文件时,将网址存储在其他字段中。

此外,我建议使用Image类直接加载图像,否则您在内存中创建一个BufferedImage和一个Image对象,大约需要。只有其中一个对象所需的内存的两倍;方法返回后,BufferedImage可立即用于垃圾收集......

private String imageUrl;

@FXML
private void handleUpload(ActionEvent t) {
    FileChooser fileChooser = new FileChooser();

    //Set extension filter


    //Show open file dialog
    File file = fileChooser.showOpenDialog(null);
    if (file != null) {
        try {
            imageUrl = file.toURI().toURL().toExternalForm();
            Image image = new Image(imageUrl);
            pic.setImage(image);
        } catch (MalformedURLException ex) {
            throw new IllegalStateException(ex);
        }
    }
}

@FXML
private void handleConfirmButtonAction(ActionEvent event) throws IOException {

    ...
    String url = imageUrl;
    ...
}

请注意,如果您在fxml中添加默认图片,则需要使用此网址初始化imageUrl

答案 1 :(得分:0)

基本的文件选择器功能是

FileChooser fileChooser = new FileChooser();
File file = fileChooser.showOpenDialog(null);

String imgAddress3;
if (file != null) {
    try {
        imgAddress3 = file.toURI().toURL().toExternalForm();
        Image image = new Image(imgAddress3);
        imageId.setImage(image);

        System.out.println(imgAddress3);
    } catch (MalformedURLException ex) {
        throw new IllegalStateException(ex);
    }
}

为澄清起见,您可以看到完整的项目。 https://github.com/ashrafulais/java_projects/tree/master/onCLickImageLoader