java - 使用目录路径创建新类型 "File"对象时出现无效 URL/非法参数异常

标签 java exception javafx illegalargumentexception

我使用目录选择器在 DirectoryChooser() 方法中选择图像目录,但在尝试选择目录时出现非法参数异常/无效 URL 异常。

ImageView imgView = new ImageView( strImageList[ count ].getPath() );

这^是对应于stacktrace中引用的第348行的代码

makeImageViewArr(folder.getAbsolutePath());

这^是与stacktrace中引用的第104行相对应的代码

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
    at javafx.scene.image.Image.validateUrl(Image.java:1118)
    at javafx.scene.image.Image.<init>(Image.java:620)
    at javafx.scene.image.ImageView.<init>(ImageView.java:166)
    at ICGPixelReader.makeImageViewArr(ICGPixelReader.java:348)
    at ICGPixelReader.DirectoryChooser(ICGPixelReader.java:104)
    at ICGPixelReader.lambda$addElements1$0(ICGPixelReader.java:205)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8411)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:380)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
    at javafx.scene.image.Image.validateUrl(Image.java:1110)
    ... 49 more

这是directoryChooser方法

//Handles when user presses button to open file chooser and select
    //the folder of images
    private void DirectoryChooser(){
        DirectoryChooser chooser = new DirectoryChooser();
        chooser.setTitle("Open images folder");
        folder = chooser.showDialog(window);

        //Null directory check
        if(folder == null){
            System.out.println("Null directory");
        }

        //Else make the image array from that folder
        else{
                System.out.println(folder);
                makeImageViewArr(folder.getAbsolutePath());
        }
    }
    //END METHOD

此目录路径将用作另一个方法(makeImageViewArr(StringfolderName))方法的参数。

//Makes imageView arraylist from all images in a given directory
    private ArrayList<ImageView> makeImageViewArr(String folderName) { 

        System.out.println(folderName);
        //transer file names from directory folder to string array
        File  imagesDir = new File(folderName);
        File[] strImageList = imagesDir.listFiles();
        myMouseHandler mouseHandler = new myMouseHandler();

        //instantiate imageview arraylist
        arrImageList = new ArrayList<ImageView>();

        //get files from folder & start at 1 to ignore ds.Store
        for( int count = 1; count < strImageList.length; count++ ) {
            ImageView imgView = new ImageView( strImageList[ count ].getPath() );
            imgView.setOnMouseClicked( mouseHandler );
            imgView.setFitHeight(500);
            imgView.setFitWidth(500);
            imgView.setPreserveRation(true);
            arrImageList.add( imgView );
        }
        return arrImageList;
    }//END METHOD

最佳答案

您将文件路径作为字符串传递给 ImageView 构造函数:

ImageView imgView = new ImageView( strImageList[ count ].getPath() );

但是,采用 StringImageView 构造函数需要一个 URL。如 documentation 中所述:

Allocates a new ImageView object with image loaded from the specified URL.

文件路径不是 URL,并且通常甚至不是有效的 URL(它可能包含非法字符,例如空格)。

因此,您应该传递从文件中获取的 URL,而不是传递路径:

ImageView imgView = new ImageView( strImageList[ count ].getURI().toString() );

顺便说一句,您的 DirectorChooser() (原文如此)方法将所选的 File 转换为 String,然后将其传递给您的makeImageViewArr(...) 方法,您可以立即从中创建一个 File 。这很奇怪(至少可以这么说)。只需更改接受文件的方法,直接传递文件即可:

private void DirectoryChooser(){
    DirectoryChooser chooser = new DirectoryChooser();
    chooser.setTitle("Open images folder");
    folder = chooser.showDialog(window);

    //Null directory check
    if(folder == null){
        System.out.println("Null directory");
    }

    //Else make the image array from that folder
    else{
            System.out.println(folder);
            makeImageViewArr(folder);
    }
}

private ArrayList<ImageView> makeImageViewArr(File imagesDir) { 

    File[] strImageList = imagesDir.listFiles();
    myMouseHandler mouseHandler = new myMouseHandler();

    //instantiate imageview arraylist
    arrImageList = new ArrayList<ImageView>();

    //get files from folder & start at 1 to ignore ds.Store
    for( int count = 1; count < strImageList.length; count++ ) {
        ImageView imgView = new ImageView( strImageList[ count ].toURI().toString() );
        imgView.setOnMouseClicked( mouseHandler );
        imgView.setFitHeight(500);
        imgView.setFitWidth(500);
        imgView.setPreserveRation(true);
        arrImageList.add( imgView );
    }
    return arrImageList;
}

关于java - 使用目录路径创建新类型 "File"对象时出现无效 URL/非法参数异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40641864/

相关文章:

java - Hibernate 没有使用 query.list() 获得正确的记录;

java - 每次加载 facelets 组件时调用 javascript 函数

python - KeyError 是适当的异常吗?

JavaFX ListView - 删除单元格之间的间距/填充/边距

JavaFX TitledPane 双边框使用 css 文件

java - 将图像图标添加到按钮/标签 Swing

java - java中字符串的LoggingOutInterceptor

Python C API : how to get string representation of exception?

c++ - xxx.exe 0xC0000005 : Access violation reading location 0xcdcdcdf1 how to debug this? 中 0x6c70f2ca 处未处理的异常

java - 为什么 javafx-maven-plugin :jlink cause a java. nio.file.AccessDeniedException?