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