9

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?

midhunhk
  • 5,560
  • 7
  • 52
  • 83
Yugesh
  • 4,030
  • 9
  • 57
  • 97
  • check this link http://stackoverflow.com/questions/9868604/how-do-i-take-a-screenshot-of-an-android-layout. – Hiren Dabhi Apr 20 '13 at 05:45
  • 1
    @HirenDabhi i get a image through this code what the link you gave,how to save it in SD card. – Yugesh Apr 20 '13 at 05:48
  • 1
    check this link to save you bitmap on sdcard. http://stackoverflow.com/questions/4263375/android-saving-created-bitmap-to-directory-on-sd-card – Hiren Dabhi Apr 20 '13 at 05:49

2 Answers2

17

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();
}
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • what is the **name** in File coding in your answer. – Yugesh Apr 20 '13 at 05:57
  • filename is the name with which you want the image to be stored. I had used hardcoded path which i edited above. – Raghunandan Apr 20 '13 at 06:00
  • it has to be quotes f = new File(file, "filename"+".png");. What do you mean by run time?. You save as jpg or gif. However png is the recommended format. – Raghunandan Apr 20 '13 at 06:36
  • thank you.finally one doubt.How to create folder and save image in that folder – Yugesh Apr 20 '13 at 07:04
  • file =new File(android.os.Environment.getExternalStorageDirectory(),"myfolder"); if(!file.exists) file.mkdirs(). creates a folder under sdcard with myfolder – Raghunandan Apr 20 '13 at 07:06
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/28547/discussion-between-raghunandan-and-yugesh) – Raghunandan Apr 20 '13 at 07:29
  • @Raghunandan Hello, with your codes, it only saved the items on the current screen. Let's say I've got a list view and there is a scroll bar, am I able to save all the data of the list view into an image? –  Dec 27 '14 at 07:09
  • You can. There is a post on stackoberflow. But i don"t have the link – Raghunandan Dec 27 '14 at 07:41
0

Above code work properly but, image override because of same name so, for avoid this problem add below line:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_hh_mm_ss");

Date now = new Date();

String fileName = formatter.format(now);

f = new File(file.getAbsolutePath()+file.separator+ "image_"+fileName+".png");
Raymond
  • 2,276
  • 2
  • 20
  • 34