private void saveLayoutAsPDFToSdcard() {
String PDF_FILE = "mnt/sdcard/invoice.pdf";
try {
View content = rootView.findViewById(R.id.ll_upperLayout);
content.setDrawingCacheEnabled(true);
content.setDrawingCacheEnabled(true);
// this is the important code :)
// Without it the view will have a dimension of 0,0 and the bitmap will be null
content.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
content.layout(0, 0, content.getMeasuredWidth(),content.getMeasuredHeight());
content.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(content.getDrawingCache());
content.setDrawingCacheEnabled(false);
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(PDF_FILE));
document.open();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
addImage(document,byteArray);
document.close();
Log.v("PRAVEENA","====saved==");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
Log.v("PRAVEENA","====failed==107"+e.toString());
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.v("PRAVEENA","====failed 11111=="+e.toString());
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void addImage(Document document, byte[] byteArray) {
Image image = null;
try
{
image = Image.getInstance(byteArray);
}
catch (BadElementException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
// image.scaleAbsolute(150f, 150f);
try
{
document.add(image);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void saveLayoutAsPNGToSdcard() {
try {
View content = rootView.findViewById(R.id.ll_upperLayout);
content.setDrawingCacheEnabled(true);
content.setDrawingCacheEnabled(true);
// this is the important code :)
// Without it the view will have a dimension of 0,0 and the bitmap will be null
content.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
content.layout(0, 0, content.getMeasuredWidth(),content.getMeasuredHeight());
content.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(content.getDrawingCache());
content.setDrawingCacheEnabled(false);
// Bitmap bitmap = content.getDrawingCache();
File file,f = null;
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.separator+ "filename"+".png");
}
FileOutputStream ostream = new FileOutputStream(f);
bitmap.compress(CompressFormat.PNG, 10, ostream);
ostream.close();
Log.v("PRAVEENA","====saved==");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
Log.v("PRAVEENA","====failed==107"+e.toString());
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.v("PRAVEENA","====failed 11111=="+e.toString());
e.printStackTrace();
}
}
To save Layout as pdf to sdcard we need
itextpdflib
This is really very helpful blog. I really like reading this blog. Thank you so much for sharing this post. It is really worth to share this post. Website Design Fort Lauderdale
ReplyDeleteThank you Shawn
Deleteplease send source code of this project
DeleteIt is a very nice blog. Really it is a very international destination post. Thanks for sharing this post.
ReplyDeleteWebsite development company in Bangladesh
Hi, please correct the selected answer, it is the one which is down and have less votes. https://stackoverflow.com/questions/4212320/get-the-current-language-in-device
ReplyDelete