我有以下每3秒运行一次的操作。
基本上它会从服务器下载一个文件并每隔3秒将其保存到一个本地文件中。
下面的代码在一段时间内完成了这项工作。Android:打开文件太多错误
public class DownloadTask extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
downloadCommandFile(eventUrl);
return null;
}
}
private void downloadCommandFile(String dlUrl){
int count;
try {
URL url = new URL(dlUrl);
NetUtils.trustAllHosts();
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.connect();
int fileSize = con.getContentLength();
Log.d(TAG, "Download file size = " + fileSize);
InputStream is = url.openStream();
String dir = Environment.getExternalStorageDirectory() + Utils.DL_DIRECTORY;
File file = new File(dir);
if(!file.exists()){
file.mkdir();
}
FileOutputStream fos = new FileOutputStream(file + Utils.DL_FILE);
byte data[] = new byte[1024];
long total = 0;
while((count = is.read(data)) != -1){
total += count;
fos.write(data, 0, count);
}
is.close();
fos.close();
con.disconnect(); // close connection
} catch (Exception e) {
Log.e(TAG, "DOWNLOAD ERROR = " + e.toString());
}
}
一切工作正常,但如果我让它运行5至10分钟,我得到以下错误。
06-04 19:40:40.872:E/NativeCrypto(6320):应用程序数据::创建管(2) 失败:打开的文件太多06-04 19:40:40.892:E/NativeCrypto (6320): AppData :: create pipe(2)失败:打开的文件过多06-04 19:40:40.892:E/EventService(6320):DOWNLOAD ERROR = javax.net.ssl.SSLException:Unable to创建应用数据
我在过去的2天里一直在做一些研究。
有建议,他们有很多连接打开,就像这个https://stackoverflow.com/a/13990490/1503155但我仍然无法弄清楚是什么问题。
任何想法可能导致问题?
在此先感谢。
你是否设法最终找出问题所在? – Bundeeteddee