android - Downloading mp3 file and storing in app's directory -


in android project, programmatically need download .mp3 file google drive download url , store in app sandbox. then, app can have play option play audio locally.

how possible achieve downloading .mp3 file server , store locally in app? later, can played local storage. on appreciated.

thank you.

you can use method:

static void downloadfile(string dwnload_file_path, string filename,         string pathtosave) {     int downloadedsize = 0;     int totalsize = 0;      try {         url url = new url(dwnload_file_path);         httpurlconnection urlconnection = (httpurlconnection) url                 .openconnection();          urlconnection.setrequestmethod("post");         urlconnection.setdooutput(true);          // connect         urlconnection.connect();          file mydir;         mydir = new file(pathtosave);         mydir.mkdirs();          // create new file, save downloaded file          string mfilename = filename;         file file = new file(mydir, mfilename);          fileoutputstream fileoutput = new fileoutputstream(file);          // stream used reading data internet         inputstream inputstream = urlconnection.getinputstream();          // total size of file downloading         totalsize = urlconnection.getcontentlength();          // runonuithread(new runnable() {         // public void run() {         // pb.setmax(totalsize);         // }         // });          // create buffer...         byte[] buffer = new byte[1024];         int bufferlength = 0;          while ((bufferlength = inputstream.read(buffer)) > 0) {             fileoutput.write(buffer, 0, bufferlength);             downloadedsize += bufferlength;             // update progressbar //             // runonuithread(new runnable() {             // public void run() {             // pb.setprogress(downloadedsize);             // float per = ((float)downloadedsize/totalsize) * 100;             // cur_val.settext("downloaded " + downloadedsize + "kb / " +             // totalsize + "kb (" + (int)per + "%)" );             // }             // });         }         // close output stream when complete //         fileoutput.close();         // runonuithread(new runnable() {         // public void run() {         // // pb.dismiss(); // if want close it..         // }         // });      } catch (final malformedurlexception e) {         // showerror("error : malformedurlexception " + e);         e.printstacktrace();     } catch (final ioexception e) {         // showerror("error : ioexception " + e);         e.printstacktrace();     } catch (final exception e) {         // showerror("error : please check internet connection " + e);     } } 

call method this:

string sdcardroot = environment.getexternalstoragedirectory()                             .tostring();                     utils.downloadfile("http://my_audio_url/my_file.mp3", "my_file.mp3",                             sdcardroot+"/myaudiofolder"); 

for playback:

string sdcardroot = environment.getexternalstoragedirectory()                         .tostring(); string audiofilepath = sdcardroot + "/myaudiofolder/my_file.mp3"; mediaplayer mplayer = new mediaplayer(); try {                 mplayer.setdatasource(audiofilepath);                 mplayer.prepare();                 mplayer.start();             } catch (ioexception e) {                 log.e("audio playback", "prepare() failed");             } 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -