【问题标题】:Opening pdf in android/kindle app在 android/kindle 应用中打开 pdf
【发布时间】:2012-06-19 21:53:38
【问题描述】:

我想从网站打开一个 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(); 
                }
            }

【问题讨论】:

    标签: android pdf kindle


    【解决方案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();
                        }
                    }
                }
            });
        }
    }
    

    【讨论】:

    • 我试过了,到目前为止我从互联网上手动下载了文件,但它仍然没有找到文件......我不确定我哪里出错了
    • 从来没有错误只是没有进入 if(file.exists()) 的 if 语句
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多