如何在Android中将PictureDrawable保存为JPEG / PNG文件

时间:2011-06-02 13:32:23

标签: java android image

我有PictureDrawable我希望保存为图像(JPEG / PNG),但我似乎无法找到任何有关如何进行此操作的信息。

我尝试了这个,但它似乎无法正常工作

PictureDrawable myDrawable = GetPictureDrawable(); 
Bitmap bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
myDrawable.draw(new Canvas(bitmap));
bitmap.compress(CompressFormat.JPEG, 100,  new FileOutputStream("/MyLocation/MyImage.jpg"));

我做错了什么?

1 个答案:

答案 0 :(得分:4)

//Convert PictureDrawable to Bitmap
private static Bitmap pictureDrawable2Bitmap(PictureDrawable pictureDrawable){
        Bitmap bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(),pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawPicture(pictureDrawable.getPicture());
        return bitmap;
    }