如何通过编码可以通过android读取PDf文件

时间:2011-09-07 09:36:10

标签: android pdf

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    Button OpenPDF = (Button) findViewById(R.id.button);
    OpenPDF.setOnClickListener(new View.OnClickListener()
    { 
        public void onClick(View v) 
        {
            File pdfFile = new File("res/raw/comic.pdf");
            if(pdfFile.exists()) 
            {
               //Uri path = Uri.fromFile(pdfFile); 
            Uri path = Uri.parse("android.resource://" + getPackageName() + "/ R.raw.comic.pdf");

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

               // Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
               // Intent pdfIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://package.name/" + pdfFile));
              //  pdfIntent.setDataAndType(path, "/assets/arduino-comic-latest.pdf");
               // pdfIntent.setType("application/pdf");
               // pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                try
                {
                    startActivity(intent);
                }
                catch(ActivityNotFoundException e)
                {
                    Toast.makeText(MainActivity.this, "No Application available to view pdf", Toast.LENGTH_LONG).show(); 
                }
            }

        }
    });

我已经完成了这段代码,但是找不到文件的路径。我必须做什么?任何人都可以帮助我

三江源

1 个答案:

答案 0 :(得分:2)

你不能直接这样做,你必须使用Native Library来阅读PDF ...库可能是iText,jPedal,Mupdf,Pdfbox ......等等Google吧