如何在Android中呈现PDF

发布于 2021-02-02 23:01:01

在我的应用程序中,我将收到一个字节流,并将其转换为手机内存中的pdf文件。如何将其渲染为pdf?并在活动中显示?

关注者
0
被浏览
591
1 个回答
  • 面试哥
    面试哥 2021-02-02
    为面试而生,有面试问题,就找面试哥。

    某些手机​​(例如Nexus One)预先安装了Quickoffice版本,因此将文件保存到SD卡后,发送适当的Intent可能很容易。

    public class OpenPdf extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            Button button = (Button) findViewById(R.id.OpenPdfButton);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    File file = new File("/sdcard/example.pdf");
    
                    if (file.exists()) {
                        Uri path = Uri.fromFile(file);
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setDataAndType(path, "application/pdf");
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    
                        try {
                            startActivity(intent);
                        } 
                        catch (ActivityNotFoundException e) {
                            Toast.makeText(OpenPdf.this, 
                                "No Application Available to View PDF", 
                                Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            });
        }
    }
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看