我有一个用于将文件上传到服务器的Android intentservice。这些文件将每1分钟添加到intentservice队列中。它在操作系统小于5.0.1的android设备中运行正常。但是在上传文件的android 5.0.1中,如果网络丢失,我没有得到任何响应,没有抛出任何异常,意图服务被阻止。文件未从该点上传。我必须重新启动应用程序才能使服务再次运行。下面是我在onHandleIntent方法中的代码。
int ReadBytes = 0;
int BUFSIZ = 4096;
byte Buf[] = new byte[BUFSIZ];
outputStream = conn.getOutputStream();
dataOutputStream = new DataOutputStream(outputStream);
fileInputStream = new FileInputStream(source);
long prevBytesRead = 0;
ReadBytes = fileInputStream.read(Buf, 0, BUFSIZ);
while(ReadBytes>0){
dataOutputStream.write(Buf, 0, ReadBytes);
prevBytesRead += ReadBytes;
long totalbytes = fileInputStream.getChannel().size();
ReadBytes = fileInputStream.read(Buf, 0, BUFSIZ);
}
outputStream.flush();
dataOutputStream.flush();
任何帮助都将不胜感激。
答案 0 :(得分:0)
您可能正在泄漏流,您应该尝试将其包装在try / catch块中:
try {
int ReadBytes = 0;
int BUFSIZ = 4096;
byte Buf[] = new byte[BUFSIZ];
outputStream = conn.getOutputStream();
dataOutputStream = new DataOutputStream(outputStream);
fileInputStream = new FileInputStream(source);
long prevBytesRead = 0;
ReadBytes = fileInputStream.read(Buf, 0, BUFSIZ);
while(ReadBytes>0){
dataOutputStream.write(Buf, 0, ReadBytes);
prevBytesRead += ReadBytes;
long totalbytes = fileInputStream.getChannel().size();
ReadBytes = fileInputStream.read(Buf, 0, BUFSIZ);
}
outputStream.flush();
dataOutputStream.flush();
} catch (IOException e) {
} finally {
// Close streams with an additional try/catch
}
也许你应该看一下Facebook的推断工具:fbinfer.com,对这些情况非常有用。