'Download files and save in application folder for android version higher than 5 (lolipop)
I'm working with a application who use downloading mp3 files from web with URL and set them in folder created in external storage ,it was work with version 5 (lolipop) ,but when I test it with version 8 and 10 ,this not work!
this is downloading code:
String surl="https://media.sd.ma/assabile/recitations_7892537823/mp3/saad-el-ghamidi-001-al-fatiha-204-9244.mp3";
try{
URL url=new URL(surl);
URLConnection conexion=url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
File apkStorage = new File(
Environment.getExternalStorageDirectory() + "/" +"apk_stokage");
if (!apkStorage.exists()) {
apkStorage.mkdir();
}
outputFile=new File(apkStorage,title+".mp3");
if (!outputFile.exists()) {
outputFile.createNewFile();
Log.e("apk_stokage","File Created");
}
String path=Environment.getExternalStorageDirectory()+"/apk_stokage/";
FileOutputStream fos = new FileOutputStream(outputFile);//Get OutputStream for NewFile Location
InputStream is= new BufferedInputStream(url.openStream());
byte[] buffer = new byte[1024];//Set buffer type
int len1 = 0;//init length
long total=0;
while ((len1 = is.read(buffer)) != -1) {
total+=len1;
publishProgress((int)((total*100)/lenghtOfFile));
fos.write(buffer, 0, len1);//Write new file
}
// fos.flush();
fos.close();
is.close();
// path2=outputFile.getPath();
}catch(Exception e){
e.printStackTrace();
}
Solution 1:[1]
I wrote a library to do that with any file type and size. you can take a look to the library. The downloaded files will be saved on download directory. Any bug fixes and improvements are welcome
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Juan Fraga |
