我目前在一个项目中使用Phonegap,我需要下载数千张图片。从现在开始,它是直接在 Javascript 中使用 Phonegap 的 API 完成的。但它需要相当长的时间才能进行(5 到 10 分钟之间)。
然后我决定用纯 Java 来完成这个任务。它运作良好!但我认为它可能会更快。
下载约 1k 图像后,应用程序开始每 4-5 秒抛出“GC_CONCURRENT FREED”,并完全停止下载 1-2 秒。我在 Eclipse 中的网络统计就像过山车一样。
这就是你们都在等待的:
我的下载类:
public class DownloadImage extends AsyncTask<String, Integer, Boolean>
{
protected static String destination = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.test.test/cache/pic_test/";
protected static Context context;
protected static String taskName;
protected static ArrayList<String> urls;
protected static int totalSize;
public DownloadImage(Context context, String taskName, ArrayList<String> urls) {
DownloadImage.context = context;
DownloadImage.taskName = taskName;
DownloadImage.urls = urls;
DownloadImage.totalSize = urls.size();
}
public DownloadImage() {}
@Override
protected Boolean doInBackground(String... params) {
String urlstring, filename;
URL url;
HttpURLConnection urlConnection;
File tmpFile;
FileOutputStream fileOutput;
InputStream inputStream;
byte[] buffer = new byte[8192];
int bufferLength;
try {
while(DownloadImage.urls.size() > 0) {
try {
urlstring = DownloadImage.urls.remove(0);
url = new URL(urlstring);
filename = urlstring.substring(urlstring.lastIndexOf("/")+1, urlstring.length());
urlConnection = (HttpURLConnection) url.openConnection();
//configuration de la connexion
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(5000);
urlConnection.connect();
tmpFile = new File(DownloadImage.destination+filename + ".temp");
fileOutput = new FileOutputStream(tmpFile);
inputStream = urlConnection.getInputStream();
bufferLength = 0;
//lecture du flux
Log.d("DownloadFile","Téléchargement en cours "+filename);
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
fileOutput.write(buffer, 0, bufferLength);
}
fileOutput.close();
inputStream.close();
((TestActivity)(DownloadImage.context)).sendJavascript("Application.testCallback('"+DownloadImage.totalSize+"', '"+(DownloadImage.totalSize - DownloadImage.urls.size() + 1)+"')");
} catch (Exception e) {
e.printStackTrace();
}
}
} catch(IndexOutOfBoundsException e) {}
return false;
}
@Override
protected void onPostExecute(Boolean result) {
if(DownloadImage.context instanceof TestActivity) {
Log.d("test_flo", "no task left in queue");
//((TestActivity) this.context).finishTask(this.taskName);
}
}
}
我的主要活动:
private class AndroidFunction {
@SuppressWarnings("unused")
private WebView mAppView;
@SuppressWarnings("unused")
private DroidGap mGap;
public AndroidFunction(DroidGap gap, WebView view) {
mGap = gap;
mAppView = view;
}
@SuppressWarnings("unused")
public String test() {
ArrayList<String> urls = new ArrayList<String>();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(getAssets().open("testpic.txt")));
String line;
while ((line = br.readLine()) != null) {
urls.add(line);
}
br.close();
} catch(Exception e) {}
new DownloadImage(TestActivity.this, "test", urls).execute();
new DownloadImage().execute();
new DownloadImage().execute();
new DownloadImage().execute();
new DownloadImage().execute();
return "";
}
}
如您所见,我尝试在每个循环之间尽可能地使用更少的对象。您是否看到它仍然抛出那些“GC_CONCURRENT FREED”的某些原因?
提前感谢您的回答!