简体   繁体   English

Android:读取pdf档案

[英]Android: read pdf file

I want use a PDFViewer in my Android app. 我想在我的Android应用程序中使用PDFViewer。 In the FirstActivity I load my pdf file in a listview, this is the code: 在FirstActivity中,我将pdf文件加载到列表视图中,这是代码:

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

}

The SecondActivity shows pdf file, but I have the error that "file is not found", this is the code: SecondActivity显示pdf文件,但出现错误“找不到文件”,这是代码:

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

How can I solve this problem? 我怎么解决这个问题?

You can use built-in app from your device using following code in your function named openPdfIntent() 您可以使用名为openPdfIntent()的函数中的以下代码从设备中使用内置应用程序

Example: 例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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