【问题标题】:Opening a PDF File in Android [duplicate]在Android中打开PDF文件[重复]
【发布时间】:2013-05-30 08:07:01
【问题描述】:

我正在尝试打开指向 pdf 文件的链接并在设备上单击按钮时显示 pdf。解决此问题的最佳方法是什么?我希望能够不使用第 3 方软件。所以我知道我可能需要将文件转换为其他文件。

【问题讨论】:

标签: android pdf file-io viewer


【解决方案1】:

在 Button 的点击监听器中试试这个:

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/example.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),”application/pdf”);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);

在使用此代码之前,您应该检查是否有任何 PDF 阅读应用程序可用。

如果您打算实现自己的 PDF 阅读器,请参考 this.

【讨论】:

    【解决方案2】:

    我认为你应该创建自己的 pdf 阅读器为此你必须下载 PDF VIEWER.jar 然后试试这段代码

    MainActivity.java

    public class MainActivity extends ListActivity {
    
        String[] pdflist;
        File[] imagelist;
        @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 < imagelist.length; i++) {
          pdflist[i] = imagelist[i].getName();
         }
         this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, pdflist));
        }
    
        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(MainActivity.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.ic_launcher;
            }
    
            public int getNextPageImageResource() {
                 return R.drawable.ic_launcher;
            }
    
            public int getZoomInImageResource() {
                 return R.drawable.ic_launcher;
            }
    
            public int getZoomOutImageResource() {
                 return R.drawable.ic_launcher;
            }
    
            public int getPdfPasswordLayoutResource() {
                 return R.drawable.ic_launcher;
            }
    
            public int getPdfPageNumberResource() {
                 return R.drawable.ic_launcher;
            }
    
            public int getPdfPasswordEditField() {
                 return R.drawable.ic_launcher;
            }
    
            public int getPdfPasswordOkButton() {
                 return R.drawable.ic_launcher;
            }
    
            public int getPdfPasswordExitButton() {
                 return R.drawable.ic_launcher;
            }
    
            public int getPdfPageNumberEditField() {
                 return R.drawable.ic_launcher;
            }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2013-11-27
      • 2016-04-05
      • 1970-01-01
      • 2013-07-01
      • 2011-05-11
      • 1970-01-01
      • 1970-01-01
      • 2019-10-10
      • 2012-05-24
      相关资源
      最近更新 更多