6

描画パーツを作成していて、指定したカメラフォルダに画像を保存するために次のコードを記述しました。ただし、アプリ名を使用して新しいフォルダーを作成し、そのフォルダーに画像を保存したいと思います。どうすればそれを作ることができますか?

また、後でその特定のフォルダから画像ファイルを取得したいと思います。

ありがとう!

現在のコード:

     String fileName = "ABC";

      // create a ContentValues and configure new image's data
      ContentValues values = new ContentValues();
      values.put(Images.Media.TITLE, fileName);
      values.put(Images.Media.DATE_ADDED, System.currentTimeMillis());
      values.put(Images.Media.MIME_TYPE, "image/jpg");

      // get a Uri for the location to save the file
      Uri uri = getContext().getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);

      try 
      {                                               
         OutputStream outStream = getContext().getContentResolver().openOutputStream(uri);

         bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);

         outStream.flush(); // empty the buffer
         outStream.close(); // close the stream
4

2 に答える 2

12

これを試してみてください。

public void saveImageToExternalStorage(Bitmap image) {
    String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/directoryName";
    try
    {
        File dir = new File(fullPath);
        if (!dir.exists()) {
        dir.mkdirs();
        }
        OutputStream fOut = null;
        File file = new File(fullPath, "image.png");
        if(file.exists())
            file.delete();
        file.createNewFile();
        fOut = new FileOutputStream(file);
        // 100 means no compression, the lower you go, the stronger the compression
        image.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        fOut.flush();
        fOut.close();
    }
    catch (Exception e)
    {
        Log.e("saveToExternalStorage()", e.getMessage());
    }
}
于 2013-01-16T17:48:44.650 に答える
-1

この方法は、何度も試した後に機能します。

private String getFilename() {
    String filepath = Environment.getExternalStorageDirectory().getPath();
    File file = new File(filepath,AUDIO_RECORDER_FOLDER);

    if(!file.exists()){
            file.mkdirs();
    }

    return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".mp3");
}
于 2014-03-10T06:57:57.120 に答える