gpt4 book ai didi

android - 如何从android中的特定URL打开pdf

转载 作者:行者123 更新时间:2023-11-30 03:27:49 28 4
gpt4 key购买 nike

我是一名 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 将来自网站,并且该网站需要在下载 PDF 之前向其发送 HTML 发布请求。我想我将能够弄清楚 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)。

关于android - 如何从android中的特定URL打开pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17954146/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com