gpt4 book ai didi

android - 下载 PDF 文件

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

在我的应用程序中,用户需要从 url 下载 PDF 文件并将其存储在 sdcard 上。但这里的问题是我只能使用 DefaultHttpClient 来访问 url。我找到了几个解决方案,但没有一个使用 DefaultHttpClient。下载 PDF 后,我还想在我的应用程序中查看 PDF。

最佳答案

//使用此方法下载pdf文件

 public void downloadPdfContent(String urlToDownload){

try {

String fileName="xyz";
String fileExtension=".pdf";

// download pdf file.

URL url = new URL(urlToDownload);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
String PATH = Environment.getExternalStorageDirectory() + "/mydownload/";
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, fileName+fileExtension);
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();

System.out.println("--pdf downloaded--ok--"+urlToDownload);
} catch (Exception e) {
e.printStackTrace();

}
}

//查看pdf使用这个方法

private void onPdfClick()
{

// String pdfFile = Environment.getExternalStorageDirectory()+ File.separator + AstroManager.file.getName();

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File("/sdcard/mydownload/xyz.pdf"), "application/*");

startActivity(intent);
}

关于android - 下载 PDF 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8384597/

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