问题Android的开放PDF:无效的文件路径 [英] Problems opening pdf in Android: Invalid file path

查看:923
本文介绍了问题Android的开放PDF:无效的文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从我的Andr​​oid应用程序打开PDF文件。我保存在应用程序包文件夹(/data/data/com.app.example/files)的PDF。我已经安装在Android模拟器中的Adobe阅读器应用程序。问题是,当我尝试打开PDF文件使用Adobe Reader模拟器显示我的下一条消息:无效文件路径

i need to open a pdf file from my android app. I have the pdf saved in the app package folder (/data/data/com.app.example/files). I have installed in the android emulator the adobe reader app. The problem is that when i try to open the pdf file with adobe reader the emulator shows me the next message: Invalid file path.

我不知道为什么会这样,但我卡住了这一点。该文件被正确保存,因为我可以在我的电脑中打开它。

I don't know why is this happening but i'm stuck on this point. The file is saved correctly, because i can open it in my computer.

下面是code:

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;
    }

先谢谢了。

问候,哈维。

推荐答案

这是因为PDF文件在你自己的包和Adobe Reader试图从你的包是不允许开发Android应用程序访问PDF文件。

This is because the PDF file resides in your own package and Adobe reader trying to access PDF file from your package which is not allowed for Android application development.

您可以尝试以节省SD卡的文件,然后打开它。

You can try to save file on SDCARD and then open it

这篇关于问题Android的开放PDF:无效的文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆