2017-01-09 8 views
1

私は、すでに私はちょうどしかしアクセスフォルダ

getClass().getResource().. 

を使用することができます知られた名前を持つ単一の画像を得るために、私は特定のフォルダに多くの画像を持っている場合はどのようなことを知っていますか?すべての単一のImage名を取得してgetResource()メソッドを呼び出す必要はありません。

デスクトップ上の次の作品は、しかし、Androidのでクラッシュが発生します。

public void initializeImages() { 

     String platform = "android"; 

     if(Platform.isIOS()) 
     { 
      platform = "ios"; 
     } else if(Platform.isDesktop()) 
     { 
      platform = "main"; 
     } 

     String path = "src/" + platform + "/resources/com/mobileapp/images/"; 


     File file = new File(path); 
     File[] allFiles = file.listFiles(); 

     for (int i = 0; i < allFiles.length; i++) { 
      Image img = null; 
      try { 
       img = ImageIO.read(allFiles[i]); 
       files.add(createImage(img)); 
      } catch (IOException ex) { 
       Logger.getLogger(ImageGroupRetriever.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } 

    } 

//Taken from a separate SO question. Not causing any issues 
public static javafx.scene.image.Image createImage(java.awt.Image image) throws IOException { 
     if (!(image instanceof RenderedImage)) { 
      BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), 
        image.getHeight(null), BufferedImage.TYPE_INT_ARGB); 
      Graphics g = bufferedImage.createGraphics(); 
      g.drawImage(image, 0, 0, null); 
      g.dispose(); 

      image = bufferedImage; 
     } 
     ByteArrayOutputStream out = new ByteArrayOutputStream(); 
     ImageIO.write((RenderedImage) image, "png", out); 
     out.flush(); 
     ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); 
     return new javafx.scene.image.Image(in); 
    } 

は、私はディレクトリ構造に検討する必要がある何か他のものはありますか?

答えて

2

あなたは、Android上でそのコードを実行する場合は、adb logcat -v threadtimeを使用して例外が表示されます。

Caused by: java.lang.NullPointerException: Attempt to get length of null array 

あなたがのためにループ内でallFiles.lengthを呼び出す行で。

Androidでは、デスクトップと同じ方法でパスを読み取ることができません。画像をsrc/android/resourcesにコピーすると、違いはありません。

build/javafxports/android /フォルダを確認するとapkが見つかるでしょう。IDE上で展開すると、画像はちょうどcom.mobileapp.imagesの下に置かれます。

だからこそ、通常のgetClass().getResource("/com/mobileapp/images/<image.png>")が機能します。

あなたができることは、すべての画像を含むzipファイルを既知の場所に追加することです。 Charm Down Storageプラグインを使用して、Android上のアプリのプライベートフォルダにジップをコピーし、画像を抽出して、最後にFile.listFilesをデバイスのプライベートパスに使用できるようになります。

これは、すべてのファイルとcom/mobileapp/imagesimages.zipという名前のzipを持って提供する、私の作品:

private List<Image> loadImages() { 
    List<Image> list = new ArrayList<>(); 

    // 1 move zip to storage 
    File dir; 
    try { 
     dir = Services.get(StorageService.class) 
       .map(s -> s.getPrivateStorage().get()) 
       .orElseThrow(() -> new IOException("Error: PrivateStorage not available")); 

     copyZip("/com/mobileapp/images/", dir.getAbsolutePath(), "images.zip"); 
    } catch (IOException ex) { 
     System.out.println("IO error " + ex.getMessage()); 
     return list; 
    } 

    // 2 unzip 
    try { 
     unzip(new File(dir, "images.zip"), new File(dir, "images")); 
    } catch (IOException ex) { 
     System.out.println("IO error " + ex.getMessage()); 
    } 

    // 3. load images 
    File images = new File(dir, "images"); 
    for (int i = 0; i < images.listFiles().length; i++) { 
     try { 
      list.add(new Image(new FileInputStream(images.listFiles()[i]))); 
     } catch (FileNotFoundException ex) { 
      System.out.println("Error " + ex.getMessage()); 
     } 

    } 
    return list; 
} 

public static void copyZip(String pathIni, String pathEnd, String name) { 
    try (InputStream myInput = BasicView.class.getResourceAsStream(pathIni + name)) { 
     String outFileName = pathEnd + "/" + name; 
     try (OutputStream myOutput = new FileOutputStream(outFileName)) { 
      byte[] buffer = new byte[1024]; 
      int length; 
      while ((length = myInput.read(buffer)) > 0) { 
       myOutput.write(buffer, 0, length); 
      } 
      myOutput.flush(); 

     } catch (IOException ex) { 
      System.out.println("Error " + ex); 
     } 
    } catch (IOException ex) { 
     System.out.println("Error " + ex); 
    } 
} 

public static void unzip(File zipFile, File targetDirectory) throws IOException { 
    try (ZipInputStream zis = new ZipInputStream(
      new BufferedInputStream(new FileInputStream(zipFile)))) { 
     ZipEntry ze; 
     int count; 
     byte[] buffer = new byte[8192]; 
     while ((ze = zis.getNextEntry()) != null) { 
      File file = new File(targetDirectory, ze.getName()); 
      File dir = ze.isDirectory() ? file : file.getParentFile(); 
      if (!dir.isDirectory() && !dir.mkdirs()) 
       throw new FileNotFoundException("Failed to ensure directory: " + dir.getAbsolutePath()); 
      if (ze.isDirectory()) 
       continue; 
      try (FileOutputStream fout = new FileOutputStream(file)) { 
       while ((count = zis.read(buffer)) != -1) 
        fout.write(buffer, 0, count); 
      } 
     } 
    } 
} 

注意これは、デスクトップとiOS上で同様に動作します。

unzipの方法は、answerに基づいています。

+0

ありがとうございました。私はちょうどそれを開始しているように私はより多くのグルーオンの質問を持っていると確信しています。私はあなたがそれのためのSOの教祖であることを見ることができます。私は助けに感謝します。 –