1

イメージビューを特定のフォルダーのイメージに保存したいのですが、このコードを試してみましたが、うまくいきません

public void saveImage(View v){  
        View content = findViewById(R.id.iv_photo);
        content.setDrawingCacheEnabled(true);
            Bitmap bitmap = content.getDrawingCache();
            //File file = new File("/DCIM/Camera/image.jpg");
            File root = Environment.getExternalStorageDirectory();
            File file = new File(root.getAbsolutePath() + "/DCIM/image.jpg");
            try {
                file.createNewFile();
                FileOutputStream ostream = new FileOutputStream(file);
                bitmap.compress(CompressFormat.JPEG, 100, ostream);
                ostream.close();
            }catch (Exception e){
                e.printStackTrace();
            }

    }

関数を呼び出すためのこのコード

saveImage(getWindow().getDecorView().findViewById(android.R.id.content));
4

1 に答える 1

1

アプリのリソースから画像ファイルを保存するには、次のように進めます。

File dest = Environment.getExternalStorageDirectory();
InputStream in = context.getResources().getDrawable(R.drawable.my_image);
// Used the File-constructor
OutputStream out = new FileOutputStream(new File(dest, "myNewImage.png"));

// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
try {
    // A little more explicit
    while ( (len = in.read(buf, 0, buf.length)) != -1){
         out.write(buf, 0, len);
    }
} finally {
    // Ensure the Streams are closed:
    in.close();
    out.close();
}

これでうまくいくはずです。

于 2013-06-18T10:42:39.000 に答える