Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading file form Assets #26

Open
chatbaz opened this issue Dec 3, 2013 · 4 comments
Open

Loading file form Assets #26

chatbaz opened this issue Dec 3, 2013 · 4 comments

Comments

@chatbaz
Copy link

chatbaz commented Dec 3, 2013

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

@Cromir
Copy link

Cromir commented Jan 2, 2014

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
Copy link

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
Copy link

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

@PetarAnastasov
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants