1

I am developing an education application which contains multiple test paper in the form of PDF file the PDF files are placed in Assets sub folder and showing in list view. to here my code is working fine. but when i am clicking on list items then the PDF file is not display showing error PDF file cannot be open (path error)

Thanks in Advance!

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.pdfviewinfo);

     AssetManager asset = getAssets();
        try {
            final String[] arrdata = asset.list("pdffolder");
            List<String> pdflist = new ArrayList<String>();
            int size = arrdata.length;
            for(int i = 0;i<size;i++)
            {
              if(arrdata[i].contains(".pdf"))
              {
                pdflist.add(arrdata[i]); 
               }
            }
            ArrayAdapter<String> adapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,pdflist);
            ListView listView = (ListView) findViewById(R.id.listView1);
            listView.setAdapter(adapter);
                    listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
             public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                 valueinfo = (String)parent.getItemAtPosition(position); 
                 File file = new File("file:///android_asset/pdffolder/"+valueinfo);
              Log.i("testing", ""+file);
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.fromFile(file),"application/pdf");
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
            }
        });
    } catch (IOException e) {

        e.printStackTrace();
    }

     }
 }
Anubian Noob
  • 13,426
  • 6
  • 53
  • 75
kishore
  • 61
  • 5
  • can you post your logcat as well? – Karthikeyan Jan 06 '15 at 05:45
  • You cannot open a pdf file directly from the assets folder. You'll need to copy it to the external storage first. Check out this answer : http://stackoverflow.com/questions/17085574/read-a-pdf-file-from-assets-folder – Shivam Verma Jan 06 '15 at 06:03

2 Answers2

0

Adobe Reader will not recognize the path when you keep the PDF File in your assets folder. When keeping PDF in your assets folder normally it will be available in your APK not on your External Storage so Adobe reader will not recognize the path. It should be kept in External Path to open it.

Karthikeyan
  • 1,119
  • 1
  • 15
  • 33
0

Copy the files from assets folder to app private memory(so that the file is protected) and use file provider to give access to the pdfs files so that , pdf reader the external app can show pdf content. https://developer.android.com/training/secure-file-sharing/setup-sharing.html

NitZRobotKoder
  • 1,046
  • 8
  • 44
  • 74