I am trying to retrieve a reference to a file stored in the assets
directory to a file named myfile.pdf
. I have tried to do it as follows:
File file = new File("android_assest/myfile.pdf);
Log.d("myTag", "" + file.isFile());
Somehow, I get false
when the myfile.pdf
do exists in the assets
directory. I verified it using getAssets().list("")
and Log.d()
each element in the returned array.
More of which, I am trying to get a reference to a PDF file and then use any PDF viewer, which is already installed on the device, in order to view the PDF.
I guess that since the previous issue (retrieving a reference to the file) returns false
then the next snipped code fails:
Intent i = new Intent(Intent.ACTION_VIEW,
Uri.parse("file:///android_asset/myfile.pdf"));
startActivity(i);
Anyone has a clue why I am unable to retrieve a reference to the file? and why I cannot use already installed PDF viewer to display a PDF (after retrieving a reference to the PDF file)?
Thanks.
like say Vipul Shah, but in the case for external.
change parts of code like these:
the before example is for Pdfs, in case of to example .txt
You can't do it like that. There is no directory structure in an apk, it is just data. The framework knows how to access it (
getAssets
), but you cannot look for it as a file in a directory.You can open it as an input stream...
Or you can copy it out of assets to internal storage or the SD card and access it there.
As Barak said you can copy it out of assets to internal storage or the SD card and open it from there using inbuilt pdf applications.
Following Snippet will help you. (I have updated this code to write to and read files from internal storage.
But i dont recommend this approach because pdf file can be more than 100mb in size.
So its not recommended to save that huge file into internal storage
Also make sure while saving file to internal storage you use
Then only other applications can read it.
Check following snippet.
Make sure to include
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
in manifest