2011-11-11 5 views
2

ボタンを押したときに新しいファイルを作成するAndroidアプリケーションを作成中です。しかし、myFile.pngが実際にディレクトリではなく.pngのファイルとして表示されているファイルはディレクトリではなく、ファイルとして表示されますか?

File file = new File(Environment.getExternalStorageDirectory() 
     +File.separator 
     +"myDirectory" //folder name 
     +File.separator 
     +"myFile.png"); //file name 
file.mkdirs(); 

:私は、次のコードを使用しています。へ:お使いの場合は、ファイルを作成したい場合は、ファイル名指定したファイルオブジェクトのパラメータでディレクトリを作成するために使用されるファイルに書き込むためのIO操作を行い、

-

答えて

5

file.mkdirs();mkdirs()であるため

File file = new File(Environment.getExternalStorageDirectory() 
    +File.separator 
    +"myDirectory" //folder name 
    +File.separator 
    +"myFile.png"); //file name 
    file.getParentFile().mkdirs(); 

これにより、外部ストレージにmyDirectoryフォルダが作成されます。

2

its documentation File.mkdirs()によると、このファイルの末尾のファイル名で指定されたディレクトリを作成します。

つまり、と明示的にという名前のディレクトリを作成します。myFile.png。それがでない場合、あなたが望むものなら、おそらくしたいfile.getParentFile().mkdirs()の代わりに。

1
file.mkdirs() 

上記のコードは、新しいディレクトリを作成します。ファイル

try{ 
    File file = new File(Environment.getExternalStorageDirectory() 
     +File.separator 
     +"myDirectory" //folder name 
     +File.separator 
     +"myFile.png"); //file name 

    myFile.createNewFile(); 
    OutputStream filoutputStream = new FileOutputStream(myFile); 
    filoutputStream.write(b); 
    filoutputStream.flush(); 
    filoutputStream.close(); 
} catch (IOException e) { 
    // handler exception 
} 
を作成するには

関連する問題