如何在没有意图的情况下从android中的sdcard打开pdf文件

时间:2014-02-11 11:43:25

标签: android-intent

请给我这个问题的任何答案

    String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download/abc.pdf";
    final Intent intent = new Intent(this, Second.class);
    intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, path);
    startActivity(intent);

1 个答案:

答案 0 :(得分:0)

我假设你使用pdfviewer库

try {
            // select a document and get bytes
            File file = new File(Environment.getExternalStorageDirectory()
                    .getAbsolutePath() + "/Download/" + "file.pdf");
            RandomAccessFile raf = new RandomAccessFile(file, "r");
            FileChannel channel = raf.getChannel();
            ByteBuffer bb = ByteBuffer.NEW(channel.map(
                    FileChannel.MapMode.READ_ONLY, 0, channel.size()));
            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);
            bp.add(base64);
            // 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);
                bp.add(base64);
                // 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());
        }

我上面的代码包含两个scenerio,

  1. 当前代码获取pdf文件并将其转换为base 64字符串并将其保存到bp(arraylist
  2. 注释代码用于在Web视图中显示输入流base 64字符串

            WebView webview = new WebView(activity);
    webview.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT));
    String html = "<!DOCTYPE html><html><body bgcolor=\"#7f7f7f\"><img src=\"data:image/png;base64,"
            + imageArray.get(position) + "\" hspace=10 vspace=10><br>";
    html += "</body></html>";
    webview.getSettings().setBuiltInZoomControls(true);
    webview.loadDataWithBaseURL("", html, "text/html", "UTF-8", "");
    
  3. 这是您可以用来在webview中显示的代码。

    或者你可以将base 64字符串转换为image,你可以使用它。

    • 不要忘记包含

      PDFImage.sShowImages = true; // show images
      PDFPaint.s_doAntiAlias = true; // make text smooth
      HardReference.sKeepCaches = true; // save images in cache
      

    如果您的pdf文件包含图像。