Java Android:读取pdf文件

Java Android:读取pdf文件,java,android,listview,pdf,pdfviewer,Java,Android,Listview,Pdf,Pdfviewer,我想在我的Android应用程序中使用PDFViewer。在第一个活动中,我将pdf文件加载到listview中,代码如下: public class MainActivity extends ActionBarActivity { final StringBuffer sb = new StringBuffer(); private ListView mainListView ; private ArrayAdapter<String> listAd

我想在我的Android应用程序中使用PDFViewer。在第一个活动中,我将pdf文件加载到listview中,代码如下:

public class MainActivity extends ActionBarActivity {

    final StringBuffer sb = new StringBuffer();

    private ListView mainListView ;  
    private ArrayAdapter<String> listAdapter ;  

    String filepath;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);

        mainListView = (ListView) findViewById( R.id.mainListView );  
        final ArrayList<String> List = new ArrayList<String>();  

        final File storage = Environment.getExternalStorageDirectory();
        File file = new File(storage,"/Folder/");

        if (file.exists() && file.isDirectory()) {
            for (String s : file.list()) {
                sb.append(s + " ");

                List.addAll( Arrays.asList(s) );
            }
        }

        listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, R.id.rowTextView,List); 
        mainListView.setAdapter( listAdapter );  

        mainListView.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

                String filepath = new File("/Folder/"+ List.get(arg2)).getAbsolutePath();

                openPdfIntent(filepath);


            }


        });
    }
    private void openPdfIntent(String path) {
            try {
                final Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, path);
                startActivity(intent);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
public class SecondActivity extends PdfViewerActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }

    public int getPreviousPageImageResource() {
        return R.drawable.left_arrow;
    }

    public int getNextPageImageResource() {
        return R.drawable.right_arrow;
    }

    public int getZoomInImageResource() {
        return R.drawable.zoom_in;
    }

    public int getZoomOutImageResource() {
        return R.drawable.zoom_out;
    }

    public int getPdfPasswordLayoutResource() {
        return R.layout.pdf_file_password;
    }

    public int getPdfPageNumberResource() {
        return R.layout.dialog_pagenumber;
    }

    public int getPdfPasswordEditField() {
        return R.id.etPassword;
    }

    public int getPdfPasswordOkButton() {
        return R.id.btOK;
    }

    public int getPdfPasswordExitButton() {
        return R.id.btExit;
    }

    public int getPdfPageNumberEditField() {
        return R.id.pagenum_edit;
    }
}

我怎样才能解决这个问题

您可以在名为openPdfIntent的函数中使用以下代码从设备使用内置应用程序

例如:

public void openPdfIntent(String filePath){
    Uri path = Uri.fromFile(filePath);
                    Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
                    pdfIntent.setDataAndType(path, "application/pdf");
                    pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    try {
                        startActivity(pdfIntent);
                    } catch (ActivityNotFoundException e) {
                        UDF.getdialog("You don't have application for PDF Viewer.",
                                activity);
                    }
    }

如果它说找不到文件,您是否调查了要传递给SecondActivity的路径?它是否真的是一个有效的路径?Android提供了内置的工具来打开你的pdf文件。你想用你自己的方式自定义它吗?Uri path=Uri.FromFilePath;不需要路径但文件我在手机上安装了Adobe Acrobat,但它没有打开我的pdf文件