简体   繁体   中英

How to read PDF files from assets sub folder in android

I have pdf files which are placed in assets sub folder. Iam showing the pdf files in the form of a list view.When clicking on any list items ,it shows an error,"Pdf cannot be displayed,Path error".How to set path for sub folder in assets???

Thanks in Advance!

Here is my class.Please check the code below 在此处输入图片说明

public class PdfViewInformation extends Activity{

ListView pdflist;
String  itemValue;
String valueinfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    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("android.resource://com.example.timesampleexp/assets/pdffolder/"+valueinfo);        
              Log.i("jduhsr", ""+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) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

     }
        File file = new File("android_asset/subfoldername/"+filename);

       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);

You have to insert your sub folder name which you are using and file name in the file path.If you want to view the pdf file you may use already installed pdf reader like adobe reader like the way i mentioned above.otherwise you can load the pdf files in your own application by using libraries like pdf.js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM