0

嗨,我有一个 DownloadFileAsync 类,用于从 url 下载文件,我从 web 完美下载文件,但是当下载完成后,我想从系统播放 mp3 文件,但我无法将地址文件从 doInBackground 发送到 onPostExecute
我的代码是:

 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;

     }

onPostExecute 代码是:

 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");

     }

如何将 aurl[1] 和 aurl[2] 从 doInBackground 发送到 onPostExecute 文件名?

4

3 回答 3

1

第一的:

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

第二次改变

protected String doInBackground(String... aurl) {  

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

.....
......

return aurl;
}  

第三次变化

protected void onPostExecute(String unused)  

protected void onPostExecute(String[] aurl) {
于 2013-12-12T21:04:43.430 回答
1

doInBackground您应该用您希望需要的物品填写您的退货OnPostExecute。例如:

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
     }
 }

你应该AsyncTask这样声明:

public class MyAsyncTask extends AsyncTask<Void, Void, List<String>> {
于 2013-12-12T20:55:23.127 回答
0

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

于 2013-12-12T20:50:41.810 回答