Android Layoutまるごと画像として保存

Android
Layoutをまるごと画像として保存する方法を紹介。
僕の場合は,FrameLayoutにいろいろとViewやキャンバスなどを重ねたものを,1枚の画像として保存したくて,次のような記述をしました。
何かの参考になればうれしいです。

 

String filepath = Environment.getExternalStorageDirectory() + "/"

                + "保存したいフォルダ名" + "/" + System.currentTimeMillis() + "jpg";

File file = new File(filepath);

file.getParentFile().mkdir();

frame.setDrawingCacheEnabled(true);

Bitmap save_bmp = Bitmap.createBitmap(frame.getDrawingCache());

try {

        FileOutputStream fos = new FileOutputStream(file, true);

        save_bmp.compress(CompressFormat.JPEG, 100, fos);

        fos.flush();

        fos.close();

        Toast.makeText(this, "保存されました。", Toast.LENGTH_SHORT).show();

        frame.setDrawingCacheEnabled(false);

} catch (Exception e) {

        Toast.makeText(this, "エラー", Toast.LENGTH_SHORT).show();

}