【问题标题】:How to open PDF in Android如何在 Android 中打开 PDF
【发布时间】:2012-05-15 21:29:35
【问题描述】:

如何从服务器打开 pdf 文件而不将其保存在设备上并且不使用任何第三方应用程序。因为我不希望我的用户下载任何应用程序来使用我的应用程序。而且我不想使用网络视图用于打开 pdf 文件。

【问题讨论】:

    标签: android


    【解决方案1】:

    此方法适用于旧版本的 android:

    在新活动中:

            WebView webview = new WebView(this);
            setContentView(webview);
            webview.getSettings().setJavaScriptEnabled(true);
            webview.getSettings().setPluginsEnabled(true);
            webview.loadUrl("http://docs.google.com/gview?embedded=true&url=URL_of_PDF/fileName.pdf");
            setContentView(webview);
    

    在清单文件中授予internet 权限。

    使用以下方法打开已经安装了 3rd 方应用程序的 PDF 文件。

    在应用程序中打开 PDF:

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

    【讨论】:

    • 我不想在 webview 中查看我的 pdf。所以请建议我如何在应用程序中打开 pdf。
    • 看,在你的问题中,你说你不想要任何 3rd 方应用程序,所以WEBVIEW 是出路。在 URL 中,您可以直接传递 PDF 所在网站的 URL。如果您知道允许 3rd 方应用程序,那么您可以选择上面示例中的intents
    • 为了使用 Intent,我必须将 pdf 文件存储在本地,但我不会将其存储在本地。我可以使用一些 jar 文件来查看 pdf 吗??
    • 为什么不想在 WEBVIEW 中打开它?对于 webview,不必将 PDF 文件放在本地驱动器上。
    • 实际上在 webView 中打开 pdf 时,我的 pdf 质量下降了。 google doc 降低了文档的质量,而且速度也很慢
    【解决方案2】:

    使用PdfViewer.jar 并编写如下代码 - 从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<imagelist.length;i++)
        {
                pdflist[i] = imagelist[i].getName();
        }
        this.setListAdapter(new ArrayAdapter<String>(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;
    }
    }
    

    希望这对您有很大帮助。尝试这个。不要忘记在清单中添加您的 Second.java。使用您的可绘制对象在 second.java 中添加任何需要的可绘制对象。并且,参考GitHub的例子

    【讨论】:

    • 你好阿加瓦尔!我能够在设备中显示 pdf 文件,但该库无法加载更大尺寸的 pdf 文件,并且 pdf 文件中的图像显示为模糊图像。你遇到过这个问题吗??
    • 一旦我开始工作,我对缺乏流畅的界面、缺乏手势和大量 nio.BufferUnderflowExceptions 感到失望,即使在我的 中添加了 android:largeHeap="true"清单中的标记
    【解决方案3】:

    为此,您必须使用 pdf 阅读器库。此链接将对您有所帮助

    https://stackoverflow.com/questions/4665957/pdf-parsing-library-for-android

    【讨论】:

    • 我无法理解如何在我的应用程序中使用该库。我对android很陌生,所以请帮助我。
    • 检查这个链接这会给你一个关于将库添加到你的项目中的简要想法stackoverflow.com/questions/3642928/…
    • 如果您不想将 pdf 存储在本地,那么您应该使用 webview 肉类...
    猜你喜欢
    • 1970-01-01
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 2016-05-30
    • 2015-09-11
    • 1970-01-01
    相关资源
    最近更新 更多