how send a parametr from a method to another android

79 views Asked by At

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 ?

3

There are 3 answers

3
Mike On BEST ANSWER

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>> {
0
EJK On

A simple solution would be to make these instance variables on your AsyncTask class. Set them in doInBackgroound, read them in onPostExecute().

0
ramaral On

First:

DownloadFileAsync extends AsyncTask<String, String, String[]>  

Second change

protected String doInBackground(String... aurl) {  

to

protected String[] doInBackground(String... aurl) {

.....
......

return aurl;
}  

Third change

protected void onPostExecute(String unused)  

to

protected void onPostExecute(String[] aurl) {