Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save the layout view as image or pdf to sd card in android?

In my application, I have a view with customer details, I want to save that view as image or PDF to SD card then print the view through third party application (Otherwise print that view directly through printer is possible).

I have no idea of how to save view as image or PDF. Can any one know please help me to solve this problem?

like image 659
Yugesh Avatar asked Apr 20 '13 05:04

Yugesh


1 Answers

Add permission in the manifest file

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Use the code below

LinearLayout content = findViewById(R.id.rlid);
content.setDrawingCacheEnabled(true);
Bitmap bitmap = content.getDrawingCache();
File file,f;                    
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) 
    {  
         file =new File(android.os.Environment.getExternalStorageDirectory(),"TTImages_cache");
         if(!file.exists())
        {
          file.mkdirs();

         } 
         f = new File(file.getAbsolutePath()+file.seperator+ "filename"+".png");
    }
  FileOutputStream ostream = new FileOutputStream(f);                                   
  bitmap.compress(CompressFormat.PNG, 10, ostream);
  ostream.close();

 } 
 catch (Exception e){
 e.printStackTrace();
}
like image 74
Raghunandan Avatar answered Nov 15 '22 15:11

Raghunandan