我最近使用Asynctask创建了一个ImageDownloader方法,现在我需要完全相同的方法来下载当前项目中的音频。你能帮我编辑这个方法吗?
(如果我在这里有一些错误,请更正我,因为我是android的新手)
这是我的ImageDownloader AsynkTask:
public class ImageDownloader extends AsyncTask<String, String, String> {
private PowerManager.WakeLock mWakeLock;
@Override
protected void onPreExecute() {
super.onPreExecute();
PowerManager pm = (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
getClass().getName());
mWakeLock.acquire();
mProgressDialog = new ProgressDialog(getActivity());
mProgressDialog.setTitle("Downloading...");
mProgressDialog.setMessage("Please wait while your file is downloading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setCancelable(true);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setMax(100);
mProgressDialog.setProgress(100);
mProgressDialog.show();
}
@Override
protected String doInBackground(String... image_urls) {
int count = 0;
try {
URL url = new URL(image_urls[0]);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
while(count != 100){
publishProgress(""+count);
count +=5;
}
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(String... progress) {
Log.v("count",progress[0]);
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String args) {
mWakeLock.release();
File showPathInToast;
File filename;
try {
String path = Environment.getExternalStorageDirectory().toString();
Log.i("in save()", "after mkdir");
new File(path + "/Dastak/News").mkdirs();
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = timeStamp;
filename = new File(path + "/Dastak/News/"+ imageFileName +".jpg");
showPathInToast = new File(path + "/Dastak/News/");
Log.i("in save()", "after file");
FileOutputStream out = new FileOutputStream(filename);
Log.i("in save()", "after outputstream");
bmImg.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
Log.i("in save()", "after outputstream closed");
// MediaStore.Images.Media.insertImage(getActivity().getContentResolver(),
//filename.getAbsolutePath(), filename.getName(),filename.getName());
Toast.makeText(getActivity().getApplicationContext(),
"File has been saved to " + showPathInToast , Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
mProgressDialog.dismiss();
}
}
答案 0 :(得分:0)
这是一个简单的AsyncTask。在构造函数中指定目标File,在调用execute时指定url。它只是读取InputStream,直到它什么都不返回,并使用FileOutputStream直接写入目标文件。
private class FileDownloadTask extends AsyncTask<String, Void, Void> {
File destination;
public FileDownloadTask(File destination) {
this.destination = destination;
}
@Override
protected Void doInBackground(String... urls) {
HttpURLConnection conn = null;
try {
URL url = new URL(urls[0]);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.connect();
if(!destination.exists()) {
if(!destination.mkdirs()) {
return null;
}
}
InputStream is = conn.getInputStream();
FileOutputStream fos = new FileOutputStream(destination);
byte[] bytes = new byte[1024];
while ((is.read(bytes) >= 0)) {
fos.write(bytes);
}
fos.flush();
fos.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(conn != null) {
conn.disconnect();
}
}
return null;
}
}
显然,您可以再次添加ProgressDialog逻辑。