How to open PDF in Android

后端 未结 3 1748
醉话见心
醉话见心 2021-02-01 11:21

How to open pdf file from server without saving it on device and without using any third party application.because i don\'t want my user to download any application to use my ap

3条回答
  •  孤街浪徒
    2021-02-01 11:49

    use PdfViewer.jar and making a code like below - copied from here

    First.java

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        File images = Environment.getExternalStorageDirectory();
        imagelist = images.listFiles(new FilenameFilter()
        {  
                public boolean accept(File dir, String name)  
                {  
                        return ((name.endsWith(".pdf")));
                }  
        }); 
        pdflist = new String[imagelist.length]; 
        for(int i = 0;i(this,android.R.layout.simple_list_item_1, pdflist));
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) 
    {
            super.onListItemClick(l, v, position, id);
            String path = imagelist[(int)id].getAbsolutePath();
            openPdfIntent(path);
    }
    
    private void openPdfIntent(String path) 
    {
        try
        {
          final Intent intent = new Intent(First.this, Second.class);
          intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, path);
          startActivity(intent);
        }
        catch (Exception e) 
        {
          e.printStackTrace();
        }
    }
    

    Second.java

    public class Second 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;
    }
    }
    

    Hope this helps you lot. Try this. Don't forgot to add your Second.java in your manifest. Add some drawables whatever it requires in second.java with your drawables. And, Refer the example from GitHub

提交回复
热议问题