如何将文件(pdf、doc等)下载到SD卡中,并在android中使用截击打开文件?
在我的android应用程序中,我需要将一个文件下载到我的SD卡中,并通过intents查看它,我在应用程序中使用的所有网络调用都是截取库。 想法。如何将文件(pdf、doc等)下载到SD卡中,并在android中使用截击打开文件?,android,file,download,android-volley,Android,File,Download,Android Volley,在我的android应用程序中,我需要将一个文件下载到我的SD卡中,并通过intents查看它,我在应用程序中使用的所有网络调用都是截取库。 想法。类下载文件fromURL扩展异步任务{ /** *在启动后台线程之前 *显示进度条对话框 * */ @凌驾 受保护的void onPreExecute(){ super.onPreExecute(); 显示对话框(进度条类型); } /** *在后台线程中下载文件 * */ @凌驾 受保护的字符串doInBackground(字符串…f_url){
类下载文件fromURL扩展异步任务{
/**
*在启动后台线程之前
*显示进度条对话框
* */
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
显示对话框(进度条类型);
}
/**
*在后台线程中下载文件
* */
@凌驾
受保护的字符串doInBackground(字符串…f_url){
整数计数;
试一试{
URL=新URL(f_URL[0]);
URLConnection conconnection=url.openConnection();
conconnect.connect();
//这将非常有用,以便您可以显示典型的0-100%进度条
int lenghtOfFile=conconnect.getContentLength();
//下载该文件
InputStream输入=新的BufferedInputStream(url.openStream(),8192);
//输出流
OutputStream output=新文件OutputStream(“/sdcard/downloaddedfile.jpg”);
字节数据[]=新字节[1024];
长总计=0;
而((计数=输入。读取(数据))!=-1){
总数+=计数;
//发布进度。。。。
//在此之后,将调用onProgressUpdate
出版进度(“+(int)((总计*100)/长度文档));
//将数据写入文件
输出.写入(数据,0,计数);
}
//冲洗输出
output.flush();
//合流
output.close();
input.close();
}捕获(例外e){
Log.e(“错误:,e.getMessage());
}
返回null;
}
/**
*更新进度条
* */
受保护的void onProgressUpdate(字符串…进度){
//设置进度百分比
pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
*完成后台任务后
*关闭进度对话框
* **/
@凌驾
受保护的void onPostExecute(字符串文件\u url){
//下载文件后关闭对话框
解雇对话框(进度条类型);
//在图像视图中显示下载的图像
//从SD卡读取图像路径
字符串imagePath=Environment.getExternalStorageDirectory().toString()+“/downloadedfile.jpg”;
//设置已下载到图像视图中
}
}
}
在我的问题中,您可以看到我想要使用volley库实现。无论如何,感谢您的回复Evolley没有提供任何下载PdfThanks等文件的请求。。工作正常。你能帮我上传文件吗…(如果支持的话可以使用截击)?是的,给我发邮件,我会给你发一些有用的上传资源。当下载进度达到50%时,如果连接丢失会发生什么情况?如果连接丢失,试着修改代码以正确断开循环
class DownloadFileFromURL extends AsyncTask<String, String, String> {
/**
* Before starting background thread
* Show Progress Bar Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
/**
* Downloading file in background thread
* */
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
// this will be useful so that you can show a tipical 0-100% progress bar
int lenghtOfFile = conection.getContentLength();
// download the file
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream
OutputStream output = new FileOutputStream("/sdcard/downloadedfile.jpg");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress(""+(int)((total*100)/lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
/**
* Updating progress bar
* */
protected void onProgressUpdate(String... progress) {
// setting progress percentage
pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
* After completing background task
* Dismiss the progress dialog
* **/
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
dismissDialog(progress_bar_type);
// Displaying downloaded image into image view
// Reading image path from sdcard
String imagePath = Environment.getExternalStorageDirectory().toString() + "/downloadedfile.jpg";
// setting downloaded into image view
}
}
}