我想使用Google Drive android Api在Google云端硬盘上制作“.ppt”文件的本地副本(外部存储)。
我正在读一个字节并将其写入我的本地文件。但是当我试图打开它时显示该文件已损坏。请帮我解决我的问题。
final DriveFile file = Drive.DriveApi.getFile(getClient(), fileid);
final Metadata[] metadata = new Metadata[1];
file.getMetadata(getClient())
.setResultCallback(new ResultCallback<DriveResource.MetadataResult>() {
@Override
public void onResult(DriveResource.MetadataResult metadataResult) {
metadata[0] = metadataResult.getMetadata();
L.b(EditorWindow.this, metadata[0].getMimeType(), 0);
}
});
file.open(getClient(), DriveFile.MODE_READ_ONLY, null)
.setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() {
@Override
public void onResult(DriveApi.DriveContentsResult result) {
if (!result.getStatus().isSuccess()) {
L.c("Error in creating new file");
return;
}
DriveContents contents = result.getDriveContents();
String filename = metadata[0].getTitle();
File localFile = new File(Environment.getExternalStorageDirectory(), filename);
OutputStream out = null;
InputStream in = null;
try {
out = new BufferedOutputStream(new FileOutputStream(localFile));
in = contents.getInputStream();
int b;
while ( (b = in.read()) > 0) {
out.write(b);
}
in.close();
out.close();
} catch (Exception e) {L.c("Error in writing to SD card");}
contents.discard(getClient());
}
});
答案 0 :(得分:5)
您可以尝试将while条件更改为((b = in.read())&gt; = 0)吗?注意,0是要从InputStream读取的有效字节。 http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read()