我正在尝试编写一个更新的简单应用程序.为此,我需要一个简单的功能,可以下载一个文件,并显示当前进度的ProgressDialog.我知道怎么做ProgressDialog,但我不知道如何显示当前进度以及如何首先下载文件.
我在从我的应用程序中下载二进制文件(视频)时遇到问题.在Quicktime中,如果我直接下载它可以正常工作但通过我的应用程序不知何故它搞砸了(即使它们在文本编辑器中看起来完全相同).这是一个例子:
URL u = new URL("http://www.path.to/a.mp4?video");
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
FileOutputStream f = new FileOutputStream(new File(root,"Video.mp4"));
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ( (len1 = in.read(buffer)) > 0 ) {
f.write(buffer);
}
f.close();
Run Code Online (Sandbox Code Playgroud) 我想从网址下载pdf文件.为了查看pdf文件,我使用了以下代码.
File file = new File("/sdcard/example.pdf");
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(OpenPdf.this, "No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
}
Run Code Online (Sandbox Code Playgroud)
它正在工作,但我如何从URL(例如http://.../example.pdf)获取pdf文件.我想从这个网址下载 pdf文件.请帮我.提前致谢.