How to download mp3 file in android from a url and save it in SD card?? Here is the code i am using

前端 未结 2 1663
感动是毒
感动是毒 2020-12-19 18:49
public void DownloadFromUrl(String imageURL, String fileName) {  
  //this is the downloader method
  try {
       URL url = new URL(\"http://picosong.com/wvaV\");
          


        
相关标签:
2条回答
  • 2020-12-19 19:28

    Try below code for download file.

    private void startDownload() {
        String url = "http://farm1.static.flickr.com/114/298125983_0e4bf66782_b.jpg";
        new DownloadFileAsync().execute(url);
    }
    
    class DownloadFileAsync extends AsyncTask<String, String, String> {
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            showDialog(DIALOG_DOWNLOAD_PROGRESS);
        }
    
        @Override
        protected String doInBackground(String... aurl) {
            int count;
            try {
                URL url = new URL(aurl[0]);
                URLConnection conexion = url.openConnection();
                conexion.connect();
                int lenghtOfFile = conexion.getContentLength();
                Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
                InputStream input = new BufferedInputStream(url.openStream());
                OutputStream output = new FileOutputStream("/sdcard/some_photo_from_gdansk_poland.jpg");
                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(String... progress) {
            Log.d("ANDRO_ASYNC",progress[0]);
            mProgressDialog.setProgress(Integer.parseInt(progress[0]));
        }
    
        @Override
        protected void onPostExecute(String unused) {
            dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
        }
    }
    

    And see below link for more information.

    Download File

    0 讨论(0)
  • 2020-12-19 19:37

    Try this code

    try{
    
    File cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"Folder Name");
    if(!cacheDir.exists())
       cacheDir.mkdirs();
    
    File f=new File(cacheDir,songname+".mp3");
    URL url = new URL(yoururl); 
    
                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();}
    
    0 讨论(0)
提交回复
热议问题