【问题标题】:Problems opening pdf in Android: Invalid file path在 Android 中打开 pdf 时出现问题:文件路径无效
【发布时间】:2011-10-25 10:23:19
【问题描述】:

我需要从我的 android 应用程序中打开一个 pdf 文件。我将pdf保存在应用程序包文件夹(/data/data/com.app.example/files)中。我已经在 android 模拟器中安装了 adobe reader 应用程序。问题是,当我尝试使用 adobe reader 打开 pdf 文件时,模拟器会显示下一条消息:文件路径无效。

我不知道为什么会这样,但我被困在这一点上。该文件已正确保存,因为我可以在我的计算机中打开它。

代码如下:

HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(url);
        try {
            HttpResponse response = client.execute(request);
            request(response, file_name);

            File file = new File("data/data/com.app.example/files/"+file_name);

            PackageManager packageManager = getPackageManager();
            Intent testIntent = new Intent(Intent.ACTION_VIEW);
            testIntent.setType("application/pdf");
            List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
            if(file.exists())
            {
                  if (list.size() > 0 && file.isFile()) {
                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_VIEW);
                        Uri uri = Uri.fromFile(file);
                        intent.setDataAndType(uri, "application/pdf");
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);     

                        startActivity(intent);
                  }else
                  {
                        System.out.println("NO APPs TO OPEN PDFs.");
                  }
            }
            else
            {
                  System.out.println("FILE DOES NOT EXIST.");
            }           
        }catch(Exception ex){
            System.out.println("Failed!");
            ex.printStackTrace();
        }


public String request(HttpResponse response, String file){
        String result = "";
        try{
            InputStream in = response.getEntity().getContent();
            FileOutputStream f = openFileOutput(file, MODE_WORLD_WRITEABLE);
            byte[] buffer = new byte[in.toString().length()];
            int len1 = 0;
            int counter = 0;
            while ((len1 = in.read(buffer)) > 0) {
                f.write(buffer, 0, len1);
                counter++;
            }
            f.close();
        }
        catch(Exception ex){
            result = "Error";
        }
        return result;
    }

提前致谢。

问候,哈维。

【问题讨论】:

    标签: java android pdf


    【解决方案1】:

    这是因为 PDF 文件驻留在您自己的包中,而 Adob​​e 阅读器试图从您的包中访问 PDF 文件,而这对于 Android 应用程序开发是不允许的。

    您可以尝试将文件保存在 SDCARD 上,然后打开它

    【讨论】:

    • 谢谢德拉克斯。这让我可以继续开发 xD。
    【解决方案2】:

    不要使用直接路径访问应用程序私有的文件。而是使用

    FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
    

    参考:http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-06
      • 2012-06-10
      相关资源
      最近更新 更多