title: Convert view to bitmap on Android
body: <p>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.</p>
  
  <pre><code> 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)
   {                    //          
      }      
    }  
  </code></pre>