4

PictureDrawable オブジェクトを Bitmap オブジェクトに変換するにはどうすればよいですか? 以下に示す次のコードを試してみましたが、null ポインターが返されます。

       //Convert PictureDrawable to Bitmap
            private Bitmap pictureDrawable2Bitmap(PictureDrawable pictureDrawable){
            Bitmap bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(),pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
            Toast.makeText(HTMLActivity.this, "bitmap " + bitmap.toString(), Toast.LENGTH_LONG).show();
            Canvas canvas = new Canvas(bitmap);
            canvas.drawPicture(pictureDrawable.getPicture());
            return bitmap;
        }

これは、コードで何をしているかを示すサンプルです。

    private Picture picture;
    private String url;
    private WebView webview;
    private Bitmap HTMLBitmap;
    private PictureDrawable HTMLPicDraw;

     // goal is to convert WebView --> Picture --> PictureDrawable --> Bitmap
        picture = webview.capturePicture();
        HTMLPicDraw = new PictureDrawable(picture);
      // HTMLPicDraw a PictureDrawable object is good, no null pointer exception here
      // what is left is to go from PictureDrawable to Bitmap
4

1 に答える 1

1

私のために働いているbeloveコードを使用してください.....

//  get bitmap from view
public static Bitmap getBitmapFromView(View view) {
            Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(returnedBitmap);
            Drawable bgDrawable =view.getBackground();
            if (bgDrawable!=null) 
                bgDrawable.draw(canvas);
          /*  else 
                canvas.drawColor(Color.WHITE);*/
            view.draw(canvas);
            return returnedBitmap;
        }
于 2013-03-21T09:27:23.870 に答える