2016-06-30 6 views
0

FileChooserで画像を選択してプログラムに表示するプログラムを作っています。しかし、私は私のフォルダ内のすべての画像を保存したい。私はそこにすべての画像を保存したい。どのようなオプションは、ユーザーがそのイメージのコピーを作成し、それを私のフォルダに貼り付けるためにデスクトップ上にあるイメージを選択する場合はありますか?javafxでFileChooserで選択したファイルをコピーして保存するには

+1

参照:http://stackoverflow.com/a/24333657/4185959 – Itai

答えて

1

実装とどのようにあなたがあなたのプログラムで開いた画像を提示するが、ここから神託の例を取っ​​ている程度全くわからないまあイム:http://docs.oracle.com/javafx/2/ui_controls/file-chooser.htm

プログラムは、特定の方向に選択したファイルをコピー作るためにそのかなり簡単javaNIOを使用して:

private void openFile(File file) { 
     try { 
      File dest = new File("C:\\Users\\yourProfile\\Desktop"); //any location 
      Files.copy(file.toPath(), dest.toPath(), StandardCopyOption.REPLACE_EXISTING); 
     } catch (IOException ex) { 
      Logger.getLogger(
       FileChooserSample.class.getName()).log(
        Level.SEVERE, null, ex 
       ); 
     } 
    } 

あなたは、私が以前のリンクサンプルアプリケーションでこれをテストすることができます。

import java.awt.Desktop; 
import java.io.File; 
import java.io.IOException; 
import java.nio.file.Files; 
import java.nio.file.StandardCopyOption; 
import java.util.List; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javafx.application.Application; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.geometry.Insets; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.layout.GridPane; 
import javafx.scene.layout.Pane; 
import javafx.scene.layout.VBox; 
import javafx.stage.FileChooser; 
import javafx.stage.Stage; 

public final class FileChooserSample extends Application { 

    private Desktop desktop = Desktop.getDesktop(); 

    @Override 
    public void start(final Stage stage) { 
     stage.setTitle("File Chooser Sample"); 

     final FileChooser fileChooser = new FileChooser(); 

     final Button openButton = new Button("Open a Picture..."); 
     final Button openMultipleButton = new Button("Open Pictures..."); 

     openButton.setOnAction(
      new EventHandler<ActionEvent>() { 
       @Override 
       public void handle(final ActionEvent e) { 
        File file = fileChooser.showOpenDialog(stage); 
        if (file != null) { 
         openFile(file); 
        } 
       } 
      }); 

     openMultipleButton.setOnAction(
      new EventHandler<ActionEvent>() { 
       @Override 
       public void handle(final ActionEvent e) { 
        List<File> list = 
         fileChooser.showOpenMultipleDialog(stage); 
        if (list != null) { 
         for (File file : list) { 
          openFile(file); 
         } 
        } 
       } 
      }); 


     final GridPane inputGridPane = new GridPane(); 

     GridPane.setConstraints(openButton, 0, 0); 
     GridPane.setConstraints(openMultipleButton, 1, 0); 
     inputGridPane.setHgap(6); 
     inputGridPane.setVgap(6); 
     inputGridPane.getChildren().addAll(openButton, openMultipleButton); 

     final Pane rootGroup = new VBox(12); 
     rootGroup.getChildren().addAll(inputGridPane); 
     rootGroup.setPadding(new Insets(12, 12, 12, 12)); 

     stage.setScene(new Scene(rootGroup)); 
     stage.show(); 
    } 

    public static void main(String[] args) { 
     Application.launch(args); 
    } 

    private void openFile(File file) { 
     try { 
      desktop.open(file); 
      File dest = new File("C:\\Users\\yourprofile\\Desktop"); 
      Files.copy(file.toPath(), dest.toPath(), StandardCopyOption.REPLACE_EXISTING); 
     } catch (IOException ex) { 
      Logger.getLogger(
       FileChooserSample.class.getName()).log(
        Level.SEVERE, null, ex 
       ); 
     } 
    } 
} 

ディレクトリを変更するだけですが、使用しているJavaのバージョンや、apache ioなどの他のライブラリを使用しているかどうかに応じて、ファイルをコピーする方法や実装が多数あります。あなたは、標準のファイル方式を使用している場合に有用である可能性が

その他のリンク:あなたのプログラムでとにかく:)幸運に助け

JavaPracticesJavaCodeGeekStackOverflow

希望。

関連する問題