I am trying to download a song from a server using URI and want to store it in res/raw folder. How can i do that in android studio.
Asked
Active
Viewed 263 times
-3
-
1Please provide some code, of whatever you've tried. – Ümañg ßürmån Sep 02 '18 at 06:33
1 Answers
0
You cant store mp3 or any file in res/raw folder , it is only read only when apk made read this, you can store file in your phone storage.
code :
try{
File newFile = newFile(android.os.Environment.getExternalStorageDirectory(),"Your Folder Name");
if(!newFile.exists())
newFile.mkdirs();
File f=new File(newFile,songname+".mp3");
URL url = new URL(yourSongUrl);
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(f);
byte data[] = new byte[1024];
long total = 0;
int count=0;
while ((count = input.read(data)) != -1) {
total++;
Log.e("while","A"+total);
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
}
Catch(Exception e){e.printStackTrace();}

jay shah
- 300
- 1
- 8