1

I am trying to extract a bitmap image from pdf. I am following this post from stack overflow Generate Thumbnail of Pdf in Android

My Pdf file is located in the assets folder

private var quaranPdfUri = "quran.pdf"

I tried the following ways to open the pdf from assets folder

1.val parcelFileDescriptor  = contentResolver.openFileDescriptor(Uri.parse("file:///android_asset/$quaranPdfUri"),"r")
2. val parcelFileDescriptor = contentResolver.openAssetFileDescriptor(Uri.parse("file:///android_asset/$quaranPdfUri"),"r")?.parcelFileDescriptor
3. val parcelFileDescriptor = contentResolver.openInputStream(Uri.parse("file:///android_asset/$quaranPdfUri"))

But in all the above cases i am getting file not found exception.

I went through a bunch of post on stack overflow to obtain uri of file present in assets folder, but i keep getting file not found exception

How to get URI from an asset File? Get Uri from file in either assets or res/raw How to pass a file path which is in assets folder to File(String path)?

BraveEvidence
  • 53
  • 11
  • 45
  • 119

1 Answers1

1

file:///android_asset/ works for WebView and little else.

Try this:

val context = TODO() // get a Context from somewhere
val assetFd = context.assets.openFd("quran.pdf")
val parcelFileDescriptor = assetFd.getParcelFileDescriptor()

Basically, use AssetManager and openFd() to get an AssetFileDescriptor, then use getParcelFileDescriptor() to get a ParcelFileDescriptor to try handing to your PdfiumCore.

If that does not work, see how he's handling it in AndroidPdfViewer. That definitely supports assets and uses PdfiumAndroid, so somehow he is getting assets to work.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491