21

このコードを使用してビットマップを外部ストレージに保存していますが、フォルダーが存在しない場合は作成されません。

String path = Environment.getExternalStorageDirectory().toString();
        OutputStream fOutputStream = null;
        File file = new File(path + "/Captures/", "screen.jpg");
        try {
            fOutputStream = new FileOutputStream(file);

            capturedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOutputStream);

            fOutputStream.flush();
            fOutputStream.close();

            MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
            return;
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
            return;
        }

画像が存在しない場合は新しいディレクトリに保存し、フォルダがデバイスにある場合はデフォルトを保存するにはどうすればよいですか?

4

6 に答える 6

41

これを試してみると、確実に結果が得られます:

String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/req_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-" + n + ".jpg";
File file = new File(myDir, fname);
Log.i(TAG, "" + file);
if (file.exists())
    file.delete();
try {
    FileOutputStream out = new FileOutputStream(file);
    bm.compress(Bitmap.CompressFormat.JPEG, 90, out);
    out.flush();
    out.close();
} catch (Exception e) {
    e.printStackTrace();
}

これを追加してギャラリーに表示します:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));

明確な答えについては、このリンクを参照してください: ギャラリーにフォルダー画像を表示する

于 2012-12-27T10:49:49.227 に答える
11

以下のコード スニペットを使用してください。

String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOutputStream = null;
File file = new File(path + "/Captures/", "screen.jpg");
if (!file.exists()) {
    file.mkdirs();
}

try {
    fOutputStream = new FileOutputStream(file);

    capturedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOutputStream);

    fOutputStream.flush();
    fOutputStream.close();

    MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
} catch (FileNotFoundException e) {
    e.printStackTrace();
    Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
    return;
} catch (IOException e) {
    e.printStackTrace();
    Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
    return;
}
于 2012-12-27T10:47:24.990 に答える
1

以下を使用します。

File dir = new File(path + "/Captures/");
if(!dir.exists()) {
    dir.mkdirs();
}
File file = new File(path + "/Captures/", "screen.jpg");
 ......
于 2012-12-27T10:45:14.487 に答える
0

File のドキュメントを参照すると、mkdir() メソッドが見つかります。UNIX のものとほとんど同じです: https://developer.android.com/reference/java/io/File.html#mkdir()

于 2012-12-27T10:45:18.347 に答える