3

我制作了一个应用程序,我可以在其中通过 URL 成功地从 Internet 下载 pdf 文件。并通过创建文件夹 app_Pdf 将它们存储到应用程序的内部存储中。但现在我想用 adobe pdf 查看器等第三方应用程序打开该文件。我已经尝试了很多方法,我已经为这个问题看了很多。我还阅读了有关制作第一个内容提供者并通过此查询提供 pdf 文件的信息:如何打开存储在内部存储器中的 PDF,但我收到错误:路径无效....

请任何朋友帮我解决这个问题。如果给我一些工作代码会很好提前非常感谢......

这就是我在内部存储器中下载和存储文件的方式:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    downloadFile("http://www.testdemo.net/testdemo/test/test/test.pdf");
}
public boolean downloadFile(String path)
{
    try
    {
        URL url = new URL(path);

        URLConnection ucon = url.openConnection();
        ucon.setReadTimeout(5000);
        ucon.setConnectTimeout(10000);

        InputStream is = ucon.getInputStream();
        BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5);

        File file = new File(getDir("Pdf", Context.MODE_WORLD_READABLE) + "/yourfile.pdf");

        if (file.exists())
        {
            file.delete();
        }
        file.createNewFile();

        FileOutputStream outStream = new FileOutputStream(file);
        byte[] buff = new byte[5 * 1024];

        int len;
        while ((len = inStream.read(buff)) != -1)
        {
            outStream.write(buff, 0, len);
        }

        outStream.flush();
        outStream.close();
        inStream.close();

    }
    catch (Exception e)
    {
        e.printStackTrace();
        return false;
    }

    return true;
}  

}

这就是我尝试从内部存储中查看 pdf 的方式:

    File file = new File(getDir("Pdf", Context.MODE_PRIVATE) + "/yourfile.pdf");
    Uri internal = Uri.fromFile(file);
    viewPdf(internal);


    private void viewPdf(Uri file) {
    Intent intent;
    intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(file, "application/pdf");

    try {
        startActivity(intent);
    } catch (ActivityNotFoundException e) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("No Application Found");
        builder.setMessage("Download one from Android Market?");
        builder.setPositiveButton("Yes, Please",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent marketIntent = new Intent(Intent.ACTION_VIEW);
                        marketIntent.setData(Uri.parse("market://details?id=com.adobe.reader"));
                        startActivity(marketIntent);
                    }
                });
        builder.setNegativeButton("No, Thanks", null);
        builder.create().show();
        Log.v("","Exception : "+e);
    }

请帮我....

4

0 回答 0