0

私はAndroidが初めてです。Eclipse でカメラ アプリケーションを使用しています。キャプチャした画像はAPPLICATION FOLDER(INTERNAL STORAGE)に保存されます。画像はJPEG形式で保存されていますが、PNG形式で保存したいです。ただし、イメージをEXTERNAL STORAGE DIRECTORYに保存したくありません。

これが私のコードです:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
        String date = dateFormat.format(new Date());
        String photoFile = "Picture_" + date + ".JPEG";

        String filename = pictureFileDir.getPath() + File.separator + photoFile;
        File pictureFile = new File(filename);    


        try {
          FileOutputStream fos = new FileOutputStream(pictureFile);
          bmp.compress(Bitmap.CompressFormat.PNG,100,fos);

          fos.flush();
          // fos.write(data);
          fos.close();
          Toast.makeText(context, "New Image saved:" + photoFile,Toast.LENGTH_LONG).show();
        } catch (Exception error) {
        //Log.d(IntersaActivity.DEBUG_TAG, "File" + filename + "not saved: "+ error.getMessage());
      Toast.makeText(context, "Image could not be saved.",
          Toast.LENGTH_LONG).show();
    }
  }

  private File getDir() {
      String filepath = "MyFileStorage";
      ContextWrapper contextWrapper = new ContextWrapper(context);
        File sdDir = contextWrapper.getDir(filepath, Context.MODE_PRIVATE);
    //File sdDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    return new File(sdDir, "CameraAPIDemo");
  }
} 
4

1 に答える 1

0

使用する

private void savePicture(String filename, Bitmap b, Context ctx){
    try {
        ObjectOutputStream oos;
        FileOutputStream out;// = new FileOutputStream(filename);
        out = ctx.openFileOutput(filename, Context.MODE_PRIVATE);
        oos = new ObjectOutputStream(out);
        b.compress(Bitmap.CompressFormat.PNG, 100, oos);

        oos.close();
        oos.notifyAll();
        out.notifyAll();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

ファイル名の拡張子をPNGに設定します

それが役に立てば幸い。

編集:このメソッドへの呼び出しを含む合計コード。

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
String date = dateFormat.format(new Date());
String photoFile = "Picture_" + date + ".PNG";

String filename = pictureFileDir.getPath() + File.separator + photoFile;   

try {
    savePicture(filename, bmp, context);
    Toast.makeText(context, "New Image saved:" + photoFile,Toast.LENGTH_LONG).show();
} catch (Exception error) {
    Toast.makeText(context, "Image could not be saved.", Toast.LENGTH_LONG).show();
}
于 2013-04-03T04:23:47.363 に答える