【问题标题】:How to open pdf file on particular area of the screen in Android如何在Android屏幕的特定区域打开pdf文件
【发布时间】:2014-03-25 11:02:19
【问题描述】:

我有一个要求,在我的屏幕标题标签上和底部 屏幕的一个按钮在那里。我想在这个标签之间显示 PDF 文件 和按钮我将如何实现这个?当我尝试打开 PDF 时,它已满 屏幕空间。

      File file = new File(getFilesDir(), "mypdf.pdf");


        try {
            fis = new FileInputStream(file);
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        //System.out.println(file.exists() + "!!");
        //InputStream in = resource.openStream();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        try {
            for (int readNum; (readNum = fis.read(buf)) != -1;) {
                bos.write(buf, 0, readNum); //no doubt here is 0
                //Writes len bytes from the specified byte array starting at offset off to this byte array output stream.
                System.out.println("read " + readNum + " bytes,");
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        byte[] bytes = bos.toByteArray();

        //below is the different part
        File someFile = new File("java2.pdf");
        try {
            FileOutputStream fos = new FileOutputStream(someFile);
            fos.write(bytes);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(
                Uri.parse("file://" + getFilesDir() + "/java2.pdf"),
                "application/pdf");

        startActivity(intent);

【问题讨论】:

    标签: android pdf webview filereader


    【解决方案1】:

    我没有尝试,但是您应该尝试在要显示 PDF 的 Ativity 中使用 Fragments。来自原始网站:

    Fragments

    Fragment 表示 Activity 中的行为或用户界面的一部分。您可以在单个活动中组合多个片段以构建多窗格 UI 并在多个活动中重用一个片段。

    在按钮和标题之间使用单个片段并在其上打开 PDF。也许对你有帮助

    【讨论】:

    • 谢谢,但在片段上我将如何显示 pdf,因为 pdf 在它自己的容器中打开。我想在我的 UI 中显示它,而不是在 pdf 容器上。
    • @MDFAIZANWAR 我认为问题在于您通过以下调用要求 PDF 对外部应用程序的孔径: Intent intent = new Intent(Intent.ACTION_VIEW);所以你不能轻易处理你的自定义视图。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-23
    • 2019-10-10
    • 1970-01-01
    • 1970-01-01
    • 2012-01-02
    相关资源
    最近更新 更多