Skip to content Skip to sidebar Skip to footer

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 tha

Solution 1:

Add permission in the manifest file

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

Use the code below

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

         } 
         f = newFile(file.getAbsolutePath()+file.seperator+ "filename"+".png");
    }
  FileOutputStreamostream=newFileOutputStream(f);                                   
  bitmap.compress(CompressFormat.PNG, 10, ostream);
  ostream.close();

 } 
 catch (Exception e){
 e.printStackTrace();
}

Solution 2:

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

SimpleDateFormatformatter=newSimpleDateFormat("yyyy_MM_dd_hh_mm_ss");

Datenow=newDate();

StringfileName= formatter.format(now);

f = newFile(file.getAbsolutePath()+file.separator+ "image_"+fileName+".png");

Post a Comment for "How To Save The Layout View As Image Or Pdf To Sd Card In Android?"