Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert a Picture object into a Bitmap object, Android

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

            }
        });
like image 252
Kevik Avatar asked Mar 22 '13 04:03

Kevik


Video Answer


1 Answers

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);
    }
});
like image 112
David Manpearl Avatar answered Sep 18 '22 14:09

David Manpearl