How to render PDF in Android

前端 未结 6 937
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 09:03

In my application I will receive a byte stream and convert it to a pdf file in the phone memory. How do I render that to a pdf? And show it on an activity?

6条回答
  •  鱼传尺愫
    2020-11-22 09:40

    Some phones (like the Nexus One) come with a version of Quickoffice pre-installed so it may be as easy as sending the appropriate Intent once you've saved the file to the SD card.

    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();
                        }
                    }
                }
            });
        }
    }
    

提交回复
热议问题