2

プログラムでディレクトリを作成し、写真を挿入しました:

File dirGallery = context.getDir("Gallery", Context.MODE_PRIVATE);
File fileWithinMyDir = new File(dirGallery, photo);
...

これは機能しています!

しかし今、私は既存のディレクトリ「Gallery」にディレクトリを作成し、他の写真を挿入したいと思います。私は試した :

File dirGallery = context.getDir("Gallery/Gallery2", Context.MODE_PRIVATE);

しかし、「ファイル...にはパス区切り文字が含まれています」というメッセージが表示されます。

私も試しました:

File dirGallery = context.getDir("Gallery", Context.MODE_PRIVATE);
dirGallery.mkdir();
File dirGallery2 = new File(dirGallery,"Gallery2");
dirGallery2.mkdir();
File fileWithinMyDir = new File(dirGallery2, nomPhoto);

そして、ファイルを取得すると:

File dirGallery = context.getDir("Gallery", Context.MODE_PRIVATE);
File dirGallery2 = new File(dirGallery,"Gallery2");
File[] listImages = dirGallery2.listFiles(filter); 

しかし、listImagesは空です。どこで失敗しましたか?

TY

4

2 に答える 2

2

これを書く代わりに:

File dirRecipe = context.getDir("Gallery/Gallery2", Context.MODE_PRIVATE);

これを試して:

File dirRecipe = context.getDir("Gallery"+File.separator+"Gallery2", Context.MODE_PRIVATE);

これにより、必要に応じてフォルダ内にフォルダが作成されます。

于 2013-02-28T18:34:26.230 に答える
2

ディレクトリ構造(例:a / b / c)をGetDir()に渡すことはできませんが、以下は機能します。

    File dir = getFilesDir();
    File dir2 = new File(dir, "test1/test2");
    dir2.mkdirs();

これにより、ディレクトリ構造が作成されます

/data/data/com.somename.someclass/files/test1/test2

于 2014-05-21T17:13:25.433 に答える