【问题标题】:Pdf file is not viewing in android appPdf 文件未在 Android 应用程序中查看
【发布时间】:2014-07-23 15:44:23
【问题描述】:

任何人都可以在此代码中提供帮助,pdf 文件未加载到应用程序中,仅显示空白屏幕,Logcat 显示 FileNotFoundExeeption: /storage/sdcard/raw/ourpdf.pdf。

我正在尝试制作一个应用程序,该应用程序将在我单击按钮时显示信息,并且每个按钮都将激活以读取特定的 pdf 文件。请提供任何具体帮助。

感谢帮助

第 1 部分

package com.code.androidpdf;

公共类 MainActivity 扩展 Activity {

//Globals:
private WebView wv;
private int ViewSize = 0;

//OnCreate Method:
@Override
protected void onCreate(Bundle savedInstanceState)

{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Settings
    PDFImage.sShowImages = true; // show images
    PDFPaint.s_doAntiAlias = true; // make text smooth
    HardReference.sKeepCaches = true; // save images in cache

    //Setup above
    wv = (WebView)findViewById(R.id.webView1);
    wv.getSettings().setBuiltInZoomControls(true);//show zoom buttons
    wv.getSettings().setSupportZoom(true);//allow zoom
    //get the width of the webview
    wv.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
    {
        @Override
        public void onGlobalLayout()
        {
            ViewSize = wv.getWidth();
            wv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        }
    });

    pdfLoadImages();//load images

}

private void pdfLoadImages() {
    try
    {
        // run async
        new AsyncTask<Void, Void, Void>()
                {
                    // create and show a progress dialog
                    ProgressDialog progressDialog = ProgressDialog.show(MainActivity.this, "", "Opening...");

                    @Override
                    protected void onPostExecute(Void result)
                    {
                        //after async close progress dialog
                        progressDialog.dismiss();
                    }

                    @Override
                    protected Void doInBackground(Void... params)
                    {
                        try
                        {
                            // select a document and get bytes
    File file = new File(Environment.getExternalStorageDirectory().getPath()+"/randompdf.pdf");
                            RandomAccessFile raf = new RandomAccessFile(file, "r");
                            FileChannel channel = raf.getChannel();
                            net.sf.andpdf.nio.ByteBuffer bb = null ;
                            raf.close();
                            // create a pdf doc
                            PDFFile pdf = new PDFFile(bb);
                            //Get the first page from the pdf doc
                            PDFPage PDFpage = pdf.getPage(1, true);
                            //create a scaling value according to the WebView Width
                            final float scale = ViewSize / PDFpage.getWidth() * 0.95f;
                            //convert the page into a bitmap with a scaling value
                            Bitmap page = PDFpage.getImage((int)(PDFpage.getWidth() * scale), (int)(PDFpage.getHeight() * scale), null, true, true);
                            //save the bitmap to a byte array
                            ByteArrayOutputStream stream = new ByteArrayOutputStream();
                            page.compress(Bitmap.CompressFormat.PNG, 100, stream);
                            stream.close();
                            byte[] byteArray = stream.toByteArray();
                            //convert the byte array to a base64 string
                            String base64 = Base64.encodeToString(byteArray, Base64.DEFAULT);
                            //create the html + add the first image to the html
                            String html = "<!DOCTYPE html><html><body bgcolor=\"#7f7f7f\"><img src=\"data:image/png;base64,"+base64+"\" hspace=10 vspace=10><br>";
                            //loop through the rest of the pages and repeat the above
                            for(int i = 2; i <= pdf.getNumPages(); i++)
                            {
                                PDFpage = pdf.getPage(i, true);
                                page = PDFpage.getImage((int)(PDFpage.getWidth() * scale), (int)(PDFpage.getHeight() * scale), null, true, true);
                                stream = new ByteArrayOutputStream();
                                page.compress(Bitmap.CompressFormat.PNG, 100, stream);
                                stream.close();
                                byteArray = stream.toByteArray();
                                base64 = Base64.encodeToString(byteArray, Base64.DEFAULT);
                                html += "<img src=\"data:image/png;base64,"+base64+"\" hspace=10 vspace=10><br>";
                            }
                            html += "</body></html>";
                            //load the html in the webview
                            wv.loadDataWithBaseURL("", html, "text/html","UTF-8", "");
                    }
                    catch (Exception e)
                    {
                        Log.d("CounterA", e.toString());
                    }
                        return null;
                    }
                }.execute();
                System.gc();// run GC
    }
    catch (Exception e)
    {
        Log.d("error", e.toString());
    }
}

}

【问题讨论】:

  • PDF文件存放在哪里?
  • 原始文件夹中保存的PDF文件
  • res> raw文件夹中保存的pdf文件

标签: pdf android


【解决方案1】:

(很遗憾)无法查看本地存储在设备中的 PDF。 Android L 已引入该功能。因此,要显示 PDF ,您有两个选择:

  1. 查看此答案以使用 webview How to open local pdf file in webview in android?(请注意,这需要互联网连接)
  2. 使用第三方 pdf 查看器。

您还可以发送意图让其他应用处理您的 pdf。

【讨论】:

  • 我正在使用github.com/jblough/Android-Pdf-Viewer-Library 这个,但不起作用,你能给我更简单的方法吗,谢谢
  • 再问一个问题,我会回答的。接受这个答案
  • 你能给我方法或提示如何通过单击按钮轻松加载 pdf 文件,我是一名初级应用程序开发人员,非常感谢你的建议
  • 这正是我的答案
【解决方案2】:

您可以使用以下方法获取文件的 InputStream

getResources().openRawResource(R.raw.ourpdf)

文档:http://developer.android.com/reference/android/content/res/Resources.html#openRawResource(int)

【讨论】:

  • 能否给我更方便的方式查看pdf,将不胜感激
  • 有一个基于MuPDF的github项目。由名为 joniks 的用户提供。看到那个。这是我想到的最容易使用的方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-02
相关资源
最近更新 更多