2016-11-16 239 views
0

我使用directorychooser在DirectoryChooser()方法中選擇圖像的目錄,但我得到非法參數異常/無效嘗試選擇目錄時的URL異常。無效的URL /非法參數使用目錄路徑創建新類型「文件」對象時出現異常

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

這^是對應於線348在堆棧跟蹤引用的代碼

makeImageViewArr(folder.getAbsolutePath()); 

這^是對應於線在堆棧跟蹤引用

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 104的代碼方法

//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))方法的參數。

//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 
+0

可以過濾這哪裏是具體到你的問題來看,它會更容易和更快對於讀者 – jthort

+0

@jthort是的!我的apolgies –

+0

@jthort我相信問題來自的兩種方法已被提取並貼出頂部 –

回答

2

你傳入的文件路徑作爲一個字符串ImageView構造:

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

然而,ImageView構造採取String期待一個URL。如documentation中所述:

分配一個新的ImageView對象和從指定URL加載的圖像。

文件路徑不是URL,通常甚至不會有效地形成爲URL(例如,它可能具有非法字符,如空白區域)。

因此,而不是傳遞路徑,你應該通過您從文件中獲取一個URL:

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

順便說一句,你DirectorChooser()(SIC)方法的選擇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; 
} 
相關問題