我的Android应用程序连接到谷歌驱动器并检查驱动器上的文件,如果该文件不存在,则将其下载到SD卡。以下代码进行下载。 发生的问题是下载的文件在下载后显示0个字节。请帮我找到代码中的错误。谢谢你帮帮忙。
private void savefile(String filename, InputStream in , Boolean replace ) {
try {
java.io.File f = new java.io.File(filename);
logonscreen("trying to savefile :"+filename);
if(replace) {
showToast("replace:"+replace.toString());
OutputStream stream = new BufferedOutputStream(new FileOutputStream(filename));
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int len = 0;
while ((len = in.read(buffer)) != -1) {
stream.write(buffer, 0, len);
}
if(stream!=null) {
stream.close();
}
} else {
showToast("replace: "+replace.toString()+" , file exists " + f.exists());
if(f.exists()== false) {
OutputStream stream = new BufferedOutputStream(new FileOutputStream(filename));
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int len = 0;
while ((len = in.read(buffer)) != -1) {
stream.write(buffer, 0, len);
}
if(stream!=null) {
stream.close();
}
}
}
//Do something
} catch (IOException e) {
// TODO: handle exception
logonscreen("exception at Save File:" +filename + "exception" + e.getMessage());
// showToast("exception:" + e.getMessage());
} catch (Exception e) {
logonscreen("exception at Save File:" + e.getMessage());
// showToast("exception:" + e.getMessage());
e.printStackTrace();
}
}