我创建了一个安卓应用程序。这需要将 pdf、音频和视频文件从我的 asp.net web api 下载到我的 android 应用程序。但我不知道我的 web api 代码会是什么样子,我的 pdf、音频文件和视频文件的 android 下载代码会是什么样子。感谢您的细读。
问问题
1012 次
1 回答
1
You can customize and use this, you will need the wakelock permission to prevent interruption
@SuppressLint("Wakelock")
public class DownloadTask extends AsyncTask<String, Integer, String>
{
private Context context;
public DownloadTask(Context context ,Dialog dialog, ProgressBar progressBar ,TextView progressTextView , String destinationPath ,String fileName , JSONObject jObject )
{
this.context = context;
}
@SuppressWarnings("resource")
@Override
protected String doInBackground(String... sUrl)
{
String directory = sUrl[0];
String fileName = sUrl[1];
//prevent CPU from going off if the user presses the power button during download
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
wl.acquire();
//download
try
{
new File(directory).mkdirs();
InputStream input = null;
OutputStream output = null;
HttpURLConnection connection = null;
try
{
//connect to url
URL url = new URL(sUrl[2]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
// check for http_ok (200)
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK)
return "Server returned HTTP "
+ connection.getResponseCode() + " "
+ connection.getResponseMessage();
int fileLength = connection.getContentLength();
// download the file
input = connection.getInputStream();
output = new FileOutputStream(directory+"/"+fileName+".mp3");//change extension
//copying
byte data[] = new byte[4096];
long total = 0;
int count;
while ((count = input.read(data)) != -1)
{
// allow canceling
if (isCancelled())
{
new File(directory+"/"+fileName+".mp3").delete();//delete partially downloaded file
return null;
}
total += count;
if (fileLength > 0 ) //publish progress only if total length is known
publishProgress( (int)(total/1024) , fileLength/1024 );//(int) (total * 100 / fileLength));
output.write(data, 0, count);
}
}
catch (Exception e)
{
return e.toString();
}
finally //closing streams and connection
{
try
{
if (output != null)
output.close();
if (input != null)
input.close();
}
catch (IOException ignored)
{
}
if (connection != null)
connection.disconnect();
}
}
finally
{
wl.release(); // release the lock screen
}
return null;
}
@Override // onPreExecute and onProgressUpdate run on ui thread so you can update ui from here
protected void onPreExecute()
{
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Integer... progress)
{
super.onProgressUpdate(progress);
}
@Override
protected void onPostExecute(String result)
{
if (result != null)
Toast.makeText(context, "Download error: " + result,Toast.LENGTH_SHORT).show();
else
{
Toast.makeText(context, " download complete ",Toast.LENGTH_SHORT).show();
}
}
}
and then call it from main
new DownloadTask(this).execute( dirPath , fileName , urlToDownload );
于 2013-09-30T12:40:30.620 回答