1

In my application i want to store a PDF file in assets and when the view button is pressed , the PDF should be viewed by 3rd party applications installed in the device.

By using the below code , i tried to access the PDF file inside assets folder but app says 'File not available' . I searched over stackoverflow for various code and none of them worked.But when i tried to extract the APK file i could be able to see the PDF file inside assets folder.

The following is the code i tried :

File file = new File("file://" + getFilesDir() + "/ccv.pdf");

            if (file.exists()) {

                Uri path = Uri.fromFile(file);
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(path, "application/pdf");
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                try {
                    startActivity(intent);
                } 
                catch (ActivityNotFoundException e) {
                    Toast.makeText(home.this, 
                        "No Application Available to View PDF", 
                        Toast.LENGTH_SHORT).show();
                }
            }
            else
            {
                Toast.makeText(home.this, 
                        "File not available", 
                        Toast.LENGTH_SHORT).show();
            }

So please provide the code snippet to access PDF file inside assets folder.

Thanks in advance

3 Answers3

1
Try this

File file = new File("file:///android_asset" + "/ccv.pdf");

            if (file.exists()) {

                Uri path = Uri.fromFile(file);
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(path, "application/pdf");
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                try {
                    startActivity(intent);
                } 
                catch (ActivityNotFoundException e) {
                    Toast.makeText(home.this, 
                        "No Application Available to View PDF", 
                        Toast.LENGTH_SHORT).show();
                }
            }
            else
            {
                Toast.makeText(home.this, 
                        "File not available", 
                        Toast.LENGTH_SHORT).show();
            }
Rohit Heera
  • 2,709
  • 2
  • 21
  • 31
0

i tried to access the PDF file inside assets folder

for that,

new File("file://" + getFilesDir() + "/ccv.pdf");  

is not going to get you the assets path.

getFilesDir() just

Returns the absolute path to the directory on the filesystem where files created

To access Assets, use getAssets() or AssetManager

You can refer how-to-access-file-under-assets-folder-in-android

Community
  • 1
  • 1
Pararth
  • 8,114
  • 4
  • 34
  • 51
0

At last i was able to find the answer , Assets cannot be accessed using File , only it can be accessed using Assetmanager

AssetManager assetManager = getAssets();
InputStream ims = assetManager.open("ccv.pdf");