9

how can I convert a Picture into a Bitmap, what I tried in the code is not working. Any Ideas on how to do this? I wanted to get the image in the Picture object and put that image into the ImageView named imageOne.

    showBitmap.setOnClickListener(new View.OnClickListener() {
        @Override
            public void onClick(View v) {

            Picture picture = wv.capturePicture();

           Bitmap bm = Bitmap.createBitmap(picture.getWidth(), 
                   picture.getHeight(), 
                   Bitmap.Config.RGB_565); 
   Canvas c = new Canvas(bm); 
   picture.draw(c);

   imageOne.setImageBitmap(bm);

            }
        });
Kevik
  • 9,181
  • 19
  • 92
  • 148
  • What is the package of your Picture class ? – Quanturium Mar 22 '13 at 05:05
  • it is android.graphics.Picture http://developer.android.com/reference/android/graphics/Picture.html – Kevik Mar 22 '13 at 05:07
  • Potentially a duplicate of http://stackoverflow.com/questions/10273646/android-how-to-convert-picture-from-webview-capturepicture-to-byte-and-bac – loeschg Mar 22 '13 at 05:10
  • Try this http://stackoverflow.com/questions/10273646/android-how-to-convert-picture-from-webview-capturepicture-to-byte-and-bac – Pragnani Mar 22 '13 at 05:11

2 Answers2

18

Add this:

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

Reference: Android - How to convert picture from webview.capturePicture() to byte[] and back to bitmap

Then modify your code as follows:

showBitmap.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Picture picture = wv.capturePicture();
        Bitmap bm = pictureDrawable2Bitmap(picture); 
        imageOne.setImageBitmap(bm);
    }
});
Community
  • 1
  • 1
David Manpearl
  • 12,362
  • 8
  • 55
  • 72
  • Thanks it works, But could you explain why `Canvas canvas = new Canvas(bitmap);canvas.drawPicture(pd.getPicture());` needed. when you already have bitmap created? – beginner Aug 06 '21 at 12:05
0
 private static Bitmap pictureDrawable2Bitmap(Picture picture) {
    final int width = picture.getwidth();
    final int height = picture.getHeight();
    Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.drawPicture(picture, new Rect(0, 0, width, height));
    return bitmap;
 }
beginner
  • 2,366
  • 4
  • 29
  • 53