android – Export screen to pdf-ThrowExceptions

Exception or error:

I’m working on an android native app that allows the user to create some exams and fill them with data. At this point I’ve created the screens to data visualization, but it would be awesome if I could export this screen containing the information to pdf, so the user could anex it on email.

I’ve seen some posts that teaches how to export a layout file to pdf, but it doesn’t include the data. Any idea how to do it?

String mFileName = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(System.currentTimeMillis());
                           String mFilePath = Environment.getExternalStorageDirectory()+ "/" + mFileName + ".pdf";
                           OutputStream out = new FileOutputStream(mFilePath);
                           LinearLayout tableLayout = findViewById(R.id.anamneseLayout);

                           Canvas bitmapCanvas = new Canvas();
                           Bitmap bitmap = Bitmap.createBitmap(tableLayout.getWidth(), tableLayout.getHeight(), Bitmap.Config.ARGB_8888);

                           bitmapCanvas.setBitmap(bitmap);
                           tableLayout.draw(bitmapCanvas);

                           // write to PNG
                           bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
How to solve:

One option is to redraw the screen to a bitmap and then print the bitmap to pdf or insert a bitmap in to a pdf document or just save the bitmap to png/jpeg.

Depending on you layout this might or might not work, if the View is something like a recyclerview or scrollview then you will get what is on screen only, if in my example it is a tablelayout containing textviews with values inside a scrollview as long as I redraw the tablelayout to the bitmap not the containing scrollview then you will get all of the table.

Basically you are redrawing your selected view and all it’s current children views to a “virtual” screen

You could wrap any views you have created in a frameLayout and then redraw the view to the bitmap instead of screen to get you a single starting point in the view hierarchy

Example how to get the whole screen as a bitmap.

            // The layout has to be Laid out and measured before drawing in to bitmap
            // If it has been draw to screen already this already been done
TableLayout tableLayout = view.findViewById(tableLayoutId);

            Canvas bitmapCanvas = new Canvas();
            Bitmap bitmap = Bitmap.createBitmap(tableLayout.getWidth(), tableLayout.getHeight(), Bitmap.Config.ARGB_8888);

            bitmapCanvas.setBitmap(bitmap);
            tableLayout.draw(bitmapCanvas);

            // Choose one of the three options below to do something with the bitmap

            // write to PNG
            // bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);

            // Or insert in to pdf Canvas (not tested)
            pdfCanvas.drawBitmap(bitmap, 0, 0 , null);

            // Or Print bitmap and then choose pdf printer (tested)
            // PrintHelper photoPrinter = new PrintHelper(this);
            // photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT);
            // photoPrinter.printBitmap(jobName, bitmap);

Leave a Reply

Your email address will not be published. Required fields are marked *