-6
A
回答
0
你可以用下面的代码,如果你是从web的
class DownloadFile extends AsyncTask<String,Integer,Long> {
ProgressDialog mProgressDialog = new ProgressDialog(MainActivity.this);// Change Mainactivity.this with your activity name.
String strFolderName;
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog.setMessage("Downloading");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setCancelable(true);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.show();
}
@Override
protected Long doInBackground(String... aurl) {
int count;
try {
URL url = new URL((String) aurl[0]);
URLConnection conexion = url.openConnection();
conexion.connect();
String targetFileName="Name"+".rar";//Change name and subname
int lenghtOfFile = conexion.getContentLength();
String PATH = Environment.getExternalStorageDirectory()+ "/"+downloadFolder+"/";
File folder = new File(PATH);
if(!folder.exists()){
folder.mkdir();//If there is no folder it will be created.
}
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(PATH+targetFileName);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress ((int)(total*100/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {}
return null;
}
protected void onProgressUpdate(Integer... progress) {
mProgressDialog.setProgress(progress[0]);
if(mProgressDialog.getProgress()==mProgressDialog.getMax()){
mProgressDialog.dismiss();
Toast.makeText(fa, "File Downloaded", Toast.LENGTH_SHORT).show();
}
}
protected void onPostExecute(String result) {
}
}
现在在您的清单fecthing网址为图像添加以下permissions-
<uses-permission android:name="android.permission.INTERNET"> </uses- permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
借鉴跟随this link
相关问题
- 1. Android应用程序在图库中保存和存储图像?
- 2. 图像保存到iPhone应用程序
- 3. 将图像保存到图库中
- 4. 将图像保存到应用程序目录中StorageStorageDirectory
- 5. 将图像保存到图库
- 6. 将图像保存到我的图库
- 7. android - 将图像保存到图库
- 8. 将图像保存到数据库的网络摄像头应用程序
- 9. 使用c将地图保存到地铁应用程序中的图像#
- 10. iPhone - UIImagePickerController - >将图像保存到应用程序文件夹
- 11. 将图像保存到网络应用程序
- 12. 将图像文件保存到IBM Bluemix Node.js应用程序
- 13. 在Android应用程序中添加许多保存的图像
- 14. 保存从res/drawable图像文件到Android应用程序中的图库应用程序
- 15. 回形针将不保存图像中的Rails应用程序
- 16. 如何在应用程序中保存图库中的图片?
- 17. 将图像保存到自定义库
- 18. Android:将图像保存到数据库
- 19. 我应该在哪里将图像保存到我的iPhone应用程序中?
- 20. 将多层CALayer图像保存到库中?
- 21. 在Windows应用商店应用程序中保存图像
- 22. C#:将多个图像保存到单个文件中
- 23. 如何使用dropzone将多个图像名称保存到数据库中?
- 24. 将图像保存到SQLite
- 25. 如何将图像保存在WPF应用程序在C#
- 26. 使用fileprovider将图像保存到图库
- 27. 如何在Android中将图像保存到图库?
- 28. 在viewpager中将图像从imageview保存到图库
- 29. 将图像保存为Base64时,应用程序在离子视图中崩溃
- 30. 如何将多个UILabels和UIImageView保存到“照片”应用中的图像