1

I have a code that convert layout to image bitmap, then attached on a PDF then it will save the file on external storage.

But I want to implement, while clicking on the download button file will be download on background and show PDF on this window.

Here is my code:

downloadPDF.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        PdfDocument pdfDocument =  new PdfDocument();
        PdfDocument.PageInfo pi = new PdfDocument.PageInfo.Builder(mBitmap.getWidth(), 
        mBitmap.getHeight(),1).create();

        PdfDocument.Page page = pdfDocument.startPage(pi);
        Canvas canvas = page.getCanvas();
        Paint paint = new Paint();
        paint.setColor(Color.parseColor("#FFFFFF"));
        canvas.drawPaint(paint);

        mBitmap = Bitmap.createScaledBitmap(mBitmap, mBitmap.getWidth(),mBitmap.getHeight(), true);
        paint.setColor(Color.BLUE);
        canvas.drawBitmap(mBitmap,0,0,null);
        pdfDocument.finishPage(page);

        if (ContextCompat.checkSelfPermission(AllotmentDoc.this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(AllotmentDoc.this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            } else {
                // No explanation needed; request the permission
                ActivityCompat.requestPermissions(AllotmentDoc.this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        MY_PERMISSIONS_WRITE_EXTERNAL_STORAGE);
            }
        } else {
            //Save PDF file

            //Create time stamp
            Date date = new Date() ;
            @SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat("_yyyy_MM_dd_HH_mm_ss").format(date);

            String filePath = Environment.getExternalStorageDirectory() +"/"+  "Allotment" +timeStamp+ ".pdf";

            File myFile = new File(filePath);

            try {
                OutputStream output = new FileOutputStream(myFile);
                pdfDocument.writeTo(output);
                output.flush();
                output.close();
                Toast.makeText(AllotmentDoc.this, "PDF Created Succesfully", Toast.LENGTH_LONG).show();
                Toast.makeText(AllotmentDoc.this, "PDF Saved On Memory", Toast.LENGTH_LONG).show();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        pdfDocument.close();
    }
});

How can I do this?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Kayes Fahim
  • 672
  • 5
  • 16
  • Does this answer your question? [Viewing pdf bytestream without saving it in Android](https://stackoverflow.com/questions/3768136/viewing-pdf-bytestream-without-saving-it-in-android) – Abdul Waheed Feb 04 '21 at 07:03
  • You could use a `WebView` or `Chrome Custom Tab` – Darshan Feb 04 '21 at 07:07
  • @DarShan Neither of which can render a PDF (without some webapp doing it). – Ryan M Feb 04 '21 at 07:36
  • @RyanM If you use `WebView` of `CCT`, you can use the online pdf viewer by google: https://docs.google.com/viewerng/viewer?url=URL_PDF_FILE_URL – Darshan Feb 04 '21 at 07:41
  • Yes, that would work (as I noted). But recommending WebView or CCTs _without_ recommending such a webapp doesn't really address the question. – Ryan M Feb 04 '21 at 07:43
  • 1
    `while clicking on the download button file will be download ` ? But the pdf file was in external storage. Downloading is done from a server not from external storage. It is pretty unclear what you have in mind. – blackapps Feb 04 '21 at 08:40
  • 1
    @blackapps bro, when the user clicks on the download button it will be download and automatically view on the same page. – Kayes Fahim Feb 05 '21 at 20:11

0 Answers0