在我的应用程序中,用户需要从网址下载PDF文件并将其存储在SD卡上。 但是这里的catch是我只能使用DefaultHttpClient来访问url。 我找到了几个解决方案,但没有使用DefaultHttpClient。 下载PDF后,我也想在我的应用程序中查看PDF。正在下载PDF文件
1
A
回答
4
从URL retreiving数据//使用这个方法来下载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);
}
0
看到http://developer.android.com/reference/org/apache/http/client/methods/HttpGet.html为与DefaultHttpClient
1
如果您必须打开PDF文件,必须在您的设备上安装pdf阅读器。否则它将显示空白屏幕。 也here是很好的例子从服务器和下载pdf查看其内容。
相关问题
- 1. 正在下载pdf文件
- 2. Objective-c:正在下载PDF文件
- 3. PDF文件下载在Vaadin
- 4. 下载PDF文件
- 5. jsPDF正在下载EMPTY PDF
- 6. 直接下载文件(pdf)
- 7. 用JavaScript下载PDF文件
- 8. pdf文件未下载
- 9. 用C#下载PDF文件
- 10. 下载PDF文件android
- 11. PDF文件开始下载
- 12. 按钮下载PDF文件
- 13. 从url下载pdf文件
- 14. IOS:HTTPRequest下载pdf文件
- 15. 限制下载PDF文件
- 16. Alamofire不下载PDF文件
- 17. 下载PDF文件提交
- 18. Python - 下载pdf文件(非.pdf)url
- 19. 在与angularjs相同的PDF文件夹中下载PDF文件
- 20. 从Zip文件中下载PDF文件
- 21. Ajax调用在jsp下载pdf文件
- 22. android:下载pdf文件并在
- 23. 在safari中下载PDF文件5.1.7
- 24. 在Android的webview下载pdf文件
- 25. 如何在python下载pdf文件?
- 26. 在Firefox中自动下载pdf文件
- 27. 在php下载pdf的文件代码
- 28. 创建pdf文件在java web下载
- 29. 无法在Silverlight下载.pdf文件
- 30. 代码在C#中下载PDF文件
正如我在我的问题中提到的,我想使用DefaulthttpClient。 在你的答案中,你已经使用HttpURLConnection – Gaurav
你可以改变它为DefaulthttpClient,最新的问题。 –
非常感谢。对不起,我花了时间。其实问题是,我的网址有一些重定向,所以我已经处理,以及..但你的代码效果不错:) – Gaurav