Search code examples
androidandroid-intentfilenamesdownloadfileasync

how send a parametr from a method to another android


hi i have a DownloadFileAsync class for download file from url i download file perfect from web but when download complete i want to play mp3 file from system but i cant send address file from doInBackground to onPostExecute
my code is :

 protected String doInBackground(String... aurl) {

         try {
             File root = Environment.getExternalStorageDirectory();
             URL u = new URL(aurl[0]);
             HttpURLConnection c = (HttpURLConnection) u.openConnection();
             c.setRequestMethod("GET");
             c.setDoOutput(true);
             c.connect();
             int lenghtOfFile = c.getContentLength();
             File myFolder = new File( root + "/download/"+aurl[1]+"/" );
             myFolder.mkdir();
             FileOutputStream f = new FileOutputStream(new File(root + "/download/"+aurl[1]+"/", aurl[2]+".mp3"));

             InputStream in = c.getInputStream();

             byte[] buffer = new byte[512];
             int len1 = 0;
             long total = 0;

             while ((len1 = in.read(buffer)) > 0) {
                 total += len1; //total = total + len1
                 publishProgress("" + (int)((total*100)/lenghtOfFile));
                 f.write(buffer, 0, len1);
             }
             f.close();
         } catch (Exception e) {
             Log.d("Downloader", e.getMessage());
         }

         return null;

     }

and onPostExecute code is :

 protected void onPostExecute(String unused) {
         mProgressDialog.dismiss();
         File musicFile2Play = new File("/sdcard/download/بابالنگ دراز/شماره یک.mp3");
         Intent i2 = new Intent();
         i2.setAction(android.content.Intent.ACTION_VIEW);
         i2.setDataAndType(Uri.fromFile(musicFile2Play), "audio/mp3");
         context.startActivity(i2);
        // intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + "hamrahtest.apk")), "application/vnd.android.package-archive");

     }

how send aurl[1] and aurl[2] from doInBackground to onPostExecute file name ?


Solution

  • You should fill your return from your doInBackground with the objects you wish your OnPostExecute need. For example:

    protected List<String> doInBackground(String... aurl) {
    
         List<String> list = new ArrayList<String>();
    
         try {
             File root = Environment.getExternalStorageDirectory();
    
             ...
    
             list.add(aurl[0]);
             list.add(aurl[1]);
             list.add(aurl[2]);
             list.add(aurl[3]);
         } catch (Exception e) {
             Log.d("Downloader", e.getMessage());
         }
    
         return list;
    
     }
    
    protected void onPostExecute(List<String> list) {
         mProgressDialog.dismiss();
    
         for (String url : list) {
            // Do you handling here
         }
     }
    

    You should declare your AsyncTask like this:

    public class MyAsyncTask extends AsyncTask<Void, Void, List<String>> {