【问题标题】:Can't Open a File Using Intent无法使用 Intent 打开文件
【发布时间】:2015-02-02 23:13:12
【问题描述】:

我在这里想要做的是。我在我的 SD 卡中找到 Pdf 文件夹并单击该文件,然后使用 Adob​​e Reader 完成操作。我成功地完成了读取 SD 卡选项,它将布局中的所有 pdf 文件显示为网格视图。但是当我单击 pdf 文件时,它会打开 Adob​​e reader 但无法打开文件。它说“无法打开该文档,因为它不是有效的 pdf 文档”。这是我的代码-

这是在 SD 卡中查找文件和文件夹的代码

public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) 
{
    View view =inflater.inflate(R.layout.magica_pdf_layout, container, false);

    // Check for SD Card
    if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) 
    {
        Toast.makeText(PdfFragment.this.getActivity(), "Error! No SDCARD Found!", Toast.LENGTH_LONG).show();

    } 

    else 
    {
        // Locate folder in your SD Card
        file = new File(Environment.getExternalStorageDirectory()+ File.separator + "Book");

        // Create a new folder if no folder named exist
        file.mkdirs();


    }

这里是onclick方法的部分

// Capture gridview item click
    grid.setOnItemClickListener(new OnItemClickListener() 
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
        {
            // get clicked file name from gridview
            String pdfname=(String)parent.getItemAtPosition(position);
             //prepare pdf file url
            selectedFile = new File(file.getAbsolutePath()+File.separator +pdfname+".pdf");
            Intent pdfIntent = new Intent();
            pdfIntent.setAction(android.content.Intent.ACTION_VIEW);
            pdfIntent.setDataAndType(Uri.fromFile(selectedFile), "application/pdf");
            startActivity(pdfIntent);
        }
    });

【问题讨论】:

  • 显示如何准备file路径?
  • file = new File(Environment.getExternalStorageDirectory()+ File.separator + "Book");文件.mkdirs();
  • 请在帖子中编辑更多代码以了解问题
  • Book 是 SD 卡中的一个文件夹,将所有 pdf 文件读入该文件夹。它读取所有 pdf 文件并在活动中显示为网格视图。当我单击 pdf 文件 adobe reader 启动但显示 pdf 文件无效的消息。
  • 看我的回答可能有帮助

标签: android pdf android-intent


【解决方案1】:

无法打开该文档,因为它不是有效的 pdf 文档

因为您没有传递 pdf 文件路径,当前仅传递所有文件所在的文件夹路径。这样做:

@Override
    public void onItemClick(AdapterView<?> parent, 
                                 View view,int position, long id) 
    {
        // get clicked file name from gridview
         String pdfname=(String)parent.getItemAtPosition(position);
        // prepare pdf file url
        File selectedFile = new File(file.getAbsolutePath()+"/"+pdfname+".pdf");
        Intent pdfIntent = new Intent();
        pdfIntent.setAction(android.content.Intent.ACTION_VIEW);
        pdfIntent.setDataAndType(Uri.fromFile(selectedFile), "application/pdf");
        startActivity(pdfIntent);
    }

【讨论】:

  • 抱歉帮不上忙。如您所见,我在 file = new File(Environment.getExternalStorageDirectory()+ File.separator + "Book"); 之前清除了文件路径所以我想我不需要写 File selectedFile = new File(file.getAbsolutePath()+"/"+pdfname+".pdf");再次。
  • URI 对象指的是文件夹(书)而不是 PDF。像 prosper 所说的那样添加 PDF 的路径。此外,请确保 PDF 位于 Book 目录中。
  • 我再次编辑我的源代码。当我运行应用程序时它关闭了。请给我一个简短的介绍。对我有帮助
  • @prospera 现在应用程序自动关闭。看我编辑我的问题添加源代码。能帮我解决就好了。
  • E/AndroidRuntime(28897): 在 com.tv.magica.pdf.PdfFragment$1.onItemClick(PdfFragment.java:113) 02-05 02:20:35.593: E/AndroidRuntime(28897) : 在 android.widget.AdapterView.performItemClick(AdapterView.java:301) 02-05 02:20:35.593: E/AndroidRuntime(28897): 在 android.widget.AbsListView.performItemClick(AbsListView.java:1507) 02-05 02:20:35.593: E/AndroidRuntime(28897): 在 android.widget.AbsListView$PerformClick.run(AbsListView.java:3292) 02-05 02:20:35.593:
猜你喜欢
  • 2015-10-18
  • 2018-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-29
  • 1970-01-01
  • 2019-01-06
  • 2010-12-17
相关资源
最近更新 更多