在android / kindle app中打开pdf

时间:2012-06-19 21:53:39

标签: android pdf kindle

我想从一个网站打开一个pdf到我正在为kindle和android制作的应用程序。

我认为我的代码是正确的,但它无法找到我的文件,我不确定是否有人知道我是否应该格式化文件不同,因为它在技术上是一个网站....

任何输入都将不胜感激。这是我到目前为止所做的:

    Button OpenPDF = (Button) findViewById(R.id.button1);
    OpenPDF.setOnClickListener(new View.OnClickListener()
    { 
        public void onClick(View v) 
        {
            File pdfFile = new File("http://thisisthewebsitewithpdf.pdf"); 
            if(pdfFile.exists()) //EXCEPTION HERE. pdfFile doesn't exist!
            {
                Uri path = Uri.fromFile(pdfFile); 
                Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
                pdfIntent.setDataAndType(path, "application/pdf");
                pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                try
                {
                    startActivity(pdfIntent);
                }
                catch(ActivityNotFoundException e)
                {
                    Toast.makeText(PDFActivity.this, "No Application available to view pdf", Toast.LENGTH_LONG).show(); 
                }
            }

1 个答案:

答案 0 :(得分:1)

您可以将pdf下载到您的SD卡,然后使用此活动打开它:

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