我在Fabric中有随机的IllegalstateException崩溃。但是无法重现。
用户从服务器下载文件。对于下载,我使用常规的HttpUrlConnection并在同时从InputStream中读取字节。在具有单独线程的前台服务中调用此方法。在反复阅读此崩溃时会发生。我只会在最后关闭InputStream,因此无法在完成之前关闭它。
Android 6、7、8、9。有趣的是100%三星,也许三星有一些特定行为?同样有趣的是,这种情况总是在下载即将结束时发生。例如,从2738688字节下载了2716156,只剩下22532。下载即将结束时所有崩溃。
public static Response downloadFile(String url, String tmpFile) throws IOException {
InputStream is = null;
OutputStream os = null;
Response result;
try {
URL urlFile = new URL(url);
HttpURLConnection connection = (HttpURLConnection) urlFile.openConnection();
connection.connect();
long size = connection.getContentLength();
int code = connection.getResponseCode();
Map<String, List<String>> responseHeaders = connection.getHeaderFields();
result = new Response(code, size, responseHeaders);
if (code != HttpURLConnection.HTTP_OK) {
return result;
}
is = connection.getInputStream();
os = new FileOutputStream(tmpFile);
final byte[] buf = new byte[1024];
int read;
//This read random crash
while ((read = is.read(buf)) != -1) {
os.write(buf, 0, read);
}
os.flush();
} finally {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
}
return result;
}