无效的URL /非法参数使用目录路径创建新类型“文件”时的异常目的 [英] Invalid URL/Illegal Argument Exception when using directory path to create a new type "File" object

查看:511
本文介绍了无效的URL /非法参数使用目录路径创建新类型“文件”时的异常目的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个directorychooser来选择DirectoryChooser()方法中的图像目录,但是当尝试选择一个目录时,我收到一个非法的参数异常/无效的URL异常。

I'm using a directorychooser to select a directory of images in the DirectoryChooser() method, yet I get a illegal argument exception/Invalid URL exception when trying to select a directory.

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

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

This^ is the code that corresponds to line 348 referenced in stacktrace

makeImageViewArr(folder.getAbsolutePath());

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

This^ is the code that corresponds to line 104 referenced in stacktrace

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

他的目录Cososer方法

Heres the directoryChooser method

//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(String folderName))方法的参数。

This directory path is to be used as parameter for another method (the makeImageViewArr(String folderName) ) method.

//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 构造函数:

You are passing the file path as a string to the ImageView constructor:

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

但是, ImageView code> String 正在期待一个URL。如文档

However, the ImageView constructor taking a String is expecting a URL. As stated in the documentation:


分配一个新的ImageView对象,并从指定的URL加载图像。

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

文件路径不是一个URL,通常甚至不能有效地形成为一个URL(例如,它可能具有非法字符,例如空格)

A file path is not a URL, and is often not even validly formed as a URL (it may have illegal characters, such as white space, for example).

所以,不要传递路径,您应该传递从文件获得的URL:

So instead of passing the path, you should pass a URL that you get from the file:

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

除此之外,您的 DirectorChooser() (sic)方法将选择的文件转换为 String ,然后将其传递给您的 makeImageViewArr(...)方法,您立即在其中创建一个文件。这是奇怪的(至少说)。只需更改接受文件的方法,并直接传递文件:

As an aside, your DirectorChooser() (sic) method converts the chosen File to a String, which you then pass to your makeImageViewArr(...) method, where you immediately create a File out of it. This is bizarre (to say the least). Just change the method to accept a file, and pass the file directly:

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;
}

这篇关于无效的URL /非法参数使用目录路径创建新类型“文件”时的异常目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆