Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to download file from server in android

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 8.00k
    Comment on it

    How to download the file from server to android phone

    It is recommended that whenever one wants to download any file from server whether it is song or any image , it must perform in the background either by using asynctask or using a thread.

    Here, we are using Asynctask:-

    /**
    * Downloading file in background thread
    */
    boolean download = true;
    
    @Override
    protected String doInBackground(String... params) 
         {
            File SDCardRoot = Environment.getExternalStorageDirectory(); // location where you want to store 
            File directory = new File(SDCardRoot, "/my_folder/"); //create directory to keep your downloaded file
            if (!directory.exists()) 
            {
              directory.mkdir();
            }
            String fileName = "mySong" + ".mp3"; //song name that will be stored in your device in case of song
            //String fileName = "myImage" + ".jpeg"; in case of image
            try 
               {
                  InputStream input = null;
                  try{
                       URL url = new URL(fileUrl); // link of the song which you want to download like (http://...)
                       input = url.openStream();
                       OutputStream output = new FileOutputStream(new File(directory, fileName));
                       download = true;
                       try {
                           byte[] buffer = new byte[1024];
                           int bytesRead = 0;
                           while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) 
                             {
                              output.write(buffer, 0, bytesRead);
                              download = true;
                             }
                          output.close();
                        }
                       catch (Exception exception) 
                        {
                         MyUtility.showLog("output exception in catch....."+ exception + "");
                         download = false;
                         output.close();
                         progressDialog.dismiss();
                        } 
                     }
                  catch (Exception exception) 
                       {
                         MyUtility.showLog("input exception in catch....."+ exception + "");
                         download = false;
                         progressDialog.dismiss();
                        } 
                 finally 
                      {
                        input.close();
                      }
             } 
           catch (Exception exception) 
              {
                download = false;
                progressDialog.dismiss();
              }
            return download;
        }
    
    @Override
    protected void onPostExecute(String status) 
         {
            if(download)
                  Toast.makeText(getApplicationContext(), "Download successfull", Toast.LENGTH_SHORT).show();
            else
                  Toast.makeText(getApplicationContext(), "Download Failed", Toast.LENGTH_SHORT).show();
         }
    

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: