簡體   English   中英

在Android應用程序中打開PDF

[英]Open PDF in android app

我正在開發一個需要在設備中打開 pdf 文件的應用程序,

我實際上在網上得到了與大多數示例相似的代碼。 但是,問題是我無法打開文件並且控件直接轉到“異常”部分。

這是下面的代碼:

public class MyPDFDemo extends Activity 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)   
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    Button OpenPDF = (Button) findViewById(R.id.button);
    OpenPDF.setOnClickListener(new View.OnClickListener()
    { 
        public void onClick(View v) 
        {
            File pdfFile = new File("/sdcard/Determine_RGB_Codes_With_Powerpoint [PDF Library].pdf"); 
            if(pdfFile.exists()) 
            {
                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(MyPDFDemo.this, "No Application available to view pdf", Toast.LENGTH_LONG).show(); 
                }
            }

        }
    });

}

當我運行此代碼時:我曾經看到“沒有可用於查看 pdf 的應用程序”。 任何人都可以請我查看pdf文件。

由於您的 catch 塊具有 ActivityNotFoundException,這意味着您沒有任何可以讀取“應用程序/pdf”文件類型格式的活動/應用程序。 從 Android Market 安裝任何 pdf 查看器(Adobe 最近發布了他們的),或者使用上面提到的開源 pdf 查看器,您的問題很可能會得到解決。

http://code.google.com/p/apv/downloads/list

https://market.android.com/details?id=cx.hell.android.pdfview&feature=search_result

當您使用給定的參數開始活動時,它會搜索所有注冊為打開 pdf 格式的應用程序/活動/意圖。 由於您的設備中沒有,您將收到 ActivityNotFoundException

首先在設備上安裝pdf閱讀器。 而不是使用此代碼從內存中讀取 pdf 文件。

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    final TextView tv = (TextView)findViewById(R.id.tv);
    Button bt=(Button)findViewById(R.id.openbtn);
    bt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

    File pdfFile = new File(Environment.getExternalStorageDirectory(),"Leave.pdf");
    if(pdfFile.exists())
    {
    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){
        tv.setText("No Application available to view PDF");
    }
    }
    else
    {
        tv.setText("File not found");
    }

        }
    });

}

您的代碼是正確的,我也使用相同的代碼在查看器中打開 pdf 文件。

由於您的設備上沒有安裝查看器,因此沒有任何查看器就無法打開。

您可以安裝適用於 Android 的Adobe 閱讀器

我無法在模擬器中打開 pdf 文件,所以我必須使用我的設備進行測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM