我有一个imageurl数组,通过DownloadFromUrl函数下载,从myfunction调用,我使用线程,我不知道有多少图像url,对于创建的每个图像下载单独线程,我想在所有这些线程之后启动一个活动结尾。
我怎么能得到所有这些线程,线程睡眠时间更长不能工作它不是一个好的过程。我也不能通过静态变量计数来计算线程结束,因为有时图像无法下载或 url 损坏,或者连接没有超时,
我现在有点迷茫,要弄清楚这些所有线程都结束了的程序是什么?
public void DownloadFromUrl(String DownloadUrl, String fileName) {
try {
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File (root.getAbsolutePath() + "/"+Imageurl.facebookpage);
if(dir.exists()==false) {
dir.mkdirs();
}
URL url = new URL(DownloadUrl); //you can write here any link
File file = new File(dir, fileName);
/* Open a connection to that URL. */
URLConnection ucon = url.openConnection();
/*
* Define InputStreams to read from the URLConnection.
*/
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
ByteArrayBuffer baf = new ByteArrayBuffer(5000);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
/* Convert the Bytes read to a String. */
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.flush();
fos.close();
LoginActivity.statsofdownload++;
Log.d("DownloadManager","file://"+file.getAbsolutePath());
} catch (IOException e) {
Imageurl.pagestat="space";
Log.d("DownloadManager", "Error: " + e);
}
}
myfunction()
{
for(String string : Imageurl.output) {
imagea++;
final int ind =imagea;
final String ss=string;
new Thread(new Runnable() {
public void run() {
DownloadFromUrl(ss,"IMAGE"+ind+".jpeg");
File root = android.os.Environment.getExternalStorageDirectory();
Imageurl.newyearsvalues.add("file://"+root.getAbsolutePath() + "/"+Imageurl.facebookpage+ "/"+"IMAGE"+ind+".jpeg");
}
}).start();
}
//// now need to call an activity but how I will know that these thread all end
}