【问题标题】:I can not open a PDF in my android application我无法在我的 Android 应用程序中打开 PDF
【发布时间】:2012-04-24 10:49:23
【问题描述】:

我做了一个安卓应用程序。 我创建了一个可扩展的列表,当我单击想要打开 PDF 的孩子时。 PDF 未打开,显示来自 no_application_found 的消息(“无法打开 PDF”)。 打开PDF的代码是:

    public boolean onChildClick (
        ExpandableListView parent, 
        View v, 
        int groupPosition,
        int childPosition,
        long id) {
    Log.d( LOG_TAG, "onChildClick: " + childPosition );

    File file = new File("http://www.ratt.ro/grafice/e2-a.pdf");
    Uri path = Uri.fromFile(file);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.setDataAndType(path, getString(R.string.application_type));
    try 
    {
        startActivity(intent);
    } 
    catch (ActivityNotFoundException e) 
    {
        Toast.makeText(ElistCBox.this, 
            getString(R.string.no_application_found), 
            Toast.LENGTH_SHORT).show();
    }

    return false;
}

【问题讨论】:

    标签: android pdf


    【解决方案1】:

    这是您在网络视图中打开 PDF 的方式:

    webview.loadUrl("http://docs.google.com/gview?embedded=true&url=http://www.ratt.ro/grafice/e2-a.pdf");
    

    【讨论】:

    【解决方案2】:

    这是我在阅读器中打开 PDF 文件的代码,如果用户没有任何 pdf 阅读器,它会打开指向 Google Play 的链接以供下载:

    void openpdf (String filename)
    
    {
        String loc = "/sdcard/";
        File file = new File(loc+filename);
        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 (Exception e) {
    
            e.printStackTrace();
            Toast.makeText(getApplicationContext(), "No pdf reader installed", Toast.LENGTH_SHORT).show();
            Intent market = new Intent(Intent.ACTION_VIEW);
            market.setData(Uri.parse("market://details?id=com.adobe.reader"));
            startActivity(market);
        }
    
    }
    

    【讨论】:

    • @SabauAndreea 您必须先下载它才能以这种方式使用它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多