Convert view to bitmap on Android

AndroidViewBitmap

Android Problem Overview


I need to convert a view to a bitmap to preview my view and to save it as an image. I tried using the following code, but it creates a blank image. I cannot understand where I made a mistake.

 View viewToBeConverted;  Bitmap viewBitmap =   Bitmap.createBitmap(viewToBeConverted.getWidth(), viewToBeConverted.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(viewBitmap); 
 viewToBeConverted.draw(canvas); 
 savephoto(“f1”, viewBitmap); 

 ////  public void savephoto(String filename,Bitmap bit)     
   {  
            File newFile = new File(Environment.getExternalStorageDirectory() + Picture_Card/"+ filename+ ".PNG");
              try 
{
                    newFile.createNewFile();                   
 try
 { 
                         FileOutputStream pdfFile = new FileOutputStream(newFile);                                                Bitmap bm = bit;                          ByteArrayOutputStream baos = new ByteArrayOutputStream();                          bm.compress(Bitmap.CompressFormat.PNG,100, baos);                                                     byte[] bytes = baos.toByteArray();                         
 pdfFile.write(bytes);                                              
      pdfFile.close();                   
 }
 catch (FileNotFoundException e) 
{                          //       
           
  }            
  } catch (IOException e)
 {                    //          
    }      
  }  

Android Solutions


Solution 1 - Android

here is my solution:

  public static Bitmap getBitmapFromView(View view) {
    	//Define a bitmap with the same size as the view
		Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
		//Bind a canvas to it
		Canvas canvas = new Canvas(returnedBitmap);
		//Get the view's background
		Drawable bgDrawable =view.getBackground();
		if (bgDrawable!=null) 
			//has background drawable, then draw it on the canvas
			bgDrawable.draw(canvas);
		else 
			//does not have background drawable, then draw white background on the canvas
			canvas.drawColor(Color.WHITE);
		// draw the view on the canvas
		view.draw(canvas);
		//return the bitmap
		return returnedBitmap;
	}

Enjoy :)

Solution 2 - Android

The most voted solution did not work for me because my view is a ViewGroup(have been inflated from a LayoutInflater). I needed to call view.measure to force the view size to be calculated in order to get the correct view size with view.getMeasuredWidth(Height).

public static Bitmap getBitmapFromView(View view) {
	view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
	Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(),
			Bitmap.Config.ARGB_8888);
	Canvas canvas = new Canvas(bitmap);
	view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
	view.draw(canvas);
	return bitmap;
}

Solution 3 - Android

use new kotlin extention function view.drawToBitmap()

mLayout.drawToBitmap()

Solution 4 - Android

Here is extension in Kotlin inspired by : Google Android Maps Utils Icon Generator

fun View.convertToBitmap(): Bitmap {
val measureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
measure(measureSpec, measureSpec)
layout(0, 0, measuredWidth, measuredHeight)
val r = Bitmap.createBitmap(measuredWidth, measuredHeight, Bitmap.Config.ARGB_8888)
r.eraseColor(Color.TRANSPARENT)
val canvas = Canvas(r)
draw(canvas)
return r }

Solution 5 - Android

All answers using drawing on the canvas won't work with a GLSurfaceView.

To capture the content of a GLSurfaceView into a bitmap you should consider to implement a custom method with gl.glReadPixels inside Renderer::onDrawFrame().

A solution snippet has been posted here.

Solution 6 - Android

Conversion of Layout or view to bitmap :

  private Bitmap createBitmapFromLayout(View tv) {        
    int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    tv.measure(spec, spec);
    tv.layout(0, 0, tv.getMeasuredWidth(), tv.getMeasuredHeight());
    Bitmap b = Bitmap.createBitmap(tv.getMeasuredWidth(), tv.measuredHeight(),
            Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    c.translate((-tv.getScrollX()), (-tv.getScrollY()));
    tv.draw(c);
    return b;
}

Without xml:

  private Bitmap createBitmapFromView() {
    TextView tv = new TextView(this);
    tv.setText("Hello Android !!");
    tv.setTextColor(Color.WHITE);
    tv.setBackgroundColor(Color.GRAY);
    int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    tv.measure(spec, spec);
    tv.layout(0, 0, tv.getMeasuredWidth(), tv.getMeasuredHeight());
    Bitmap b = Bitmap.createBitmap(tv.getMeasuredWidth(), tv.measuredHeight(),
            Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    c.translate((-tv.getScrollX()), (-tv.getScrollY()));
    tv.draw(c);
    return b;
}

Solution 7 - Android

since API level 24 there is a better way with PixelCopy example of use:

/**
 * use [PixelCopy] to take a snap shoot of the given view
 */
fun copyViewPixelsToBitmap(window: Window?, view: View): Bitmap {
    val snap = Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888)

    val listener = PixelCopy.OnPixelCopyFinishedListener {
        if (it != PixelCopy.SUCCESS) {
            Log.d("copyViewPixelsToBitmap failed with error: $it")
        }
    }
    val xy = IntArray(2)
    view.getLocationInWindow(xy)

    val rect = Rect(xy[0], xy[1], xy[0] + view.width, xy[1] + view.height)
    window?.let {
        PixelCopy.request(it, rect, snap, listener,
            Handler(Looper.getMainLooper()))
    }
    return snap
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSAM BhadaniView Question on Stackoverflow
Solution 1 - AndroidGil SHView Answer on Stackoverflow
Solution 2 - AndroiddangelView Answer on Stackoverflow
Solution 3 - AndroidkedView Answer on Stackoverflow
Solution 4 - AndroidMichał JanowiczView Answer on Stackoverflow
Solution 5 - AndroidrockeyeView Answer on Stackoverflow
Solution 6 - AndroidShyam KumarView Answer on Stackoverflow
Solution 7 - AndroidYosefView Answer on Stackoverflow