繁体   English   中英

如何从Android中的特定URL打开PDF

[英]how to open a pdf from specific URL in android

我是iOS开发人员,但还负责更新公司的android应用程序(因此我几乎没有android经验)android应用程序当前从原始​​加载PDF,然后将其显示在也安装在android上的另一个pdf阅读器应用程序中...但是我想从互联网上获取pdf。

这是用于显示本地存储的pdf的代码。

          if (mExternalStorageAvailable==true && mExternalStorageWriteable==true)
      {
        // Create a path where we will place our private file on external
            // storage.
            Context context1 = getApplicationContext();
            File file = new File(context1.getExternalFilesDir(null).toString() + "/pdf.pdf");

            URL url = new URL("https://myurl/pdf.pdf");
            URLConnection urlConnection = url.openConnection();
            InputStream in = new BufferedInputStream(urlConnection.getInputStream());
            OutputStream os = new FileOutputStream(file);
            try {


                byte[] data = new byte[in.available()];
                in.read(data);
                os.write(data);




                Uri path = Uri.fromFile(file);
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(path, "application/pdf");
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                startActivity(intent);

            } catch (IOException e) {
                // Unable to create file, likely because external storage is
                // not currently mounted.
                Log.w("ExternalStorage", "Error writing " + file, e);


                Context context2 = getApplicationContext();
                CharSequence text1 = "PDF File NOT Saved";
                int duration1 = Toast.LENGTH_SHORT;
                  Toast toast = Toast.makeText(context2, text1, duration1);
                  toast.show();
            } finally {
                 in.close();
                 os.close();
            }

      }

最终,pdf将来自某个网​​站,并且该网站需要发送HTML帖子请求才能下载PDF。 我想我可以弄清楚HTML帖子,但是现在我如何才能从互联网上下载PDF并显示出来。 我尝试将URI更改为指向该位置,但这没有用,或者结构不正确。

另外出于安全原因也请记住,我不想使用Google Viewer和WebView显示此内容

您只需要从远程服务器读取即可。 我会尝试类似的东西:

URL url = new URL("http://www.mydomain.com/slug");
URLConnection urlConnection = url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
try {
    readStream(in); // Process your pdf
} finally {
    in.close();
}

您可能还需要检出AndroidHttpClient类以直接发出http请求(在应用程序中为GET或POST)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM