嗨,我有一个 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 文件名?