3

I have a pdf file in Assets folder and would like to open it up. I need to create a File as below in order to set as a ParcelFileDescriptor to pass to the PdfRenderer.

 private val pdfRenderer by lazy { val file = File(cacheDir, "tempFile") file.outputStream().use { fileOut -> assets.open("TestPdf.pdf").copyTo(fileOut) } val fileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY) PdfRenderer(fileDescriptor) } 

This seems lame, as I need to create the temp file, even though I have the physical file in my Assets Folder. Is there a way to skip creating the temp file in cache?

Extra info: I tried using the below

 private val pdfRenderer by lazy { PdfRenderer(assets.openFd("TestPdf.pdf").parcelFileDescriptor) } 

But I got this error Caused by: java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed.

I put this in build.gradle

 aaptOptions { noCompress "pdf" } 

And got this error Caused by: java.io.IOException: file not in PDF format or corrupted

2
  • Can not the pdf renderer read from an inputstream? Commented Aug 10, 2020 at 14:10
  • @blackapps, I am looking for that too. That works for me as well if you have a solution. Do PdfRenderer seems to take in File only. :( Commented Aug 10, 2020 at 23:39

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.