9

如何将图片转换为位图,我在代码中尝试的内容不起作用。关于如何做到这一点的任何想法?我想在 Picture 对象中获取图像并将该图像放入名为 imageOne 的 ImageView 中。

    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);

            }
        });
4

1 回答 1

18

添加这个:

//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;
}

参考:Android - 如何将图片从 webview.capturePicture() 转换为 byte[] 并返回位图

然后修改你的代码如下:

showBitmap.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Picture picture = wv.capturePicture();
        Bitmap bm = pictureDrawable2Bitmap(picture); 
        imageOne.setImageBitmap(bm);
    }
});
于 2013-03-22T05:55:49.530 回答