jblough / Android-Pdf-Viewer-Library

DEPRECATED - Pdf Viewer library for Android
657 stars 329 forks source link

Loading file form Assets #26

Open chatbaz opened 10 years ago

chatbaz commented 10 years ago

It doesn't open files from Assets. When the PDF activity open it shows a Loading dialog and do nothing!

Cromir commented 10 years ago

PDF cannot be loaded from Assets folder. You should copy it on sdcard before opening it. http://stackoverflow.com/questions/4447477/android-how-to-copy-files-in-assets-to-sdcard

pratikbutani commented 10 years ago

I have uploaded code which is tested on device and emulator also.

you may see following link:

https://www.dropbox.com/s/zl1adgsd5adyl3k/OpenAssetsPDF.zip

abrar-sair-confiz commented 9 years ago

I have downloaded pdf file saved on external storage, its still showing loading indicator and not showing pdf

PetarAnastasov commented 9 years ago

The files in the assets directory doesent get unpacked. Instead they are read directly from the .apk file. Instead you'll have to extract the asset and write it to a seperate file and then get it's path and add it as extra in the intent.

Something like this:

public String getAssetsPdfPath(Context context) { String filePath = context.getFilesDir() + File.separator + "myFile.pdf"; File destinationFile = new File(filePath);

try { FileOutputStream outputStream = new FileOutputStream(destinationFile); InputStream inputStream = context.getAssets().open("myFile.pdf"); byte[] buffer = new byte[1024]; int length = 0; while ((length = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, length); } outputStream.close(); inputStream.close(); } catch (IOException e) { Log.e(context.getClass().getSimpleName(), "Error."); }

return destinationFile.getPath(); }

And that's it. Hope this helps anyone. Cheers.