我只是无法管理从谷歌驱动器下载文件,我刚刚创建。我可以获得webContentLink。如果我通过浏览器访问此文件,我可以下载文件,但是我无法通过我的应用程序下载相同的文件...我几乎阅读了有关谷歌驱动器的所有文档,但我找不到任何方式将文件下载到我的存储。使用google-api-services-drive从谷歌驱动器下载文件:v3
在这里,我得到的链接文件...我能得到我想要的东西永远是这样
downloadLink = m.getAlternateLink();
downloadLink = m.getEmbedLink();
downloadLink = m.getWebContentLink();
downloadLink = m.getWebViewLink();
或类似驱动器Id所有其他参数...
这里是我的代码来获得它的驱动器上的文件的信息:
Query query2 = new Query.Builder()
.addFilter(Filters.and(Filters.eq(
SearchableField.TITLE, "locations.csv"),
Filters.eq(SearchableField.TRASHED, false)))
.build();
Drive.DriveApi.query(mGoogleApiClient, query2)
.setResultCallback(new ResultCallback<DriveApi.MetadataBufferResult>() {
@Override
public void onResult(DriveApi.MetadataBufferResult result) {
if (!result.getStatus().isSuccess()) {
System.out.println("File does not exists");
} else {
for(Metadata m : result.getMetadataBuffer()) {
if (m.getTitle().equals("locations.csv")) {
downloadLink = m.getWebContentLink();
}
}
}
}
});
在这个文件夹中,只有一个文件,我确信。所以我只需要一种方法将这个文件下载到SD卡或更好的特定文件夹中。
欢迎任何帮助!谢谢。
我的解决方案
在点击代码中的某处
driveFileMy.open(mGoogleApiClient, DriveFile.MODE_READ_ONLY, null)
.setResultCallback(contentsOpenedCallback);
那么我这样做:
final private ResultCallback<DriveApi.DriveContentsResult> contentsOpenedCallback =
new ResultCallback<DriveApi.DriveContentsResult>() {
@Override
public void onResult(DriveApi.DriveContentsResult result) {
if (!result.getStatus().isSuccess()) {
// display an error saying file can't be opened
return;
}
// DriveContents object contains pointers
// to the actual byte stream
DriveContents contents = result.getDriveContents();
InputStream inputStream = contents.getInputStream();
OutputStream out = null;
try {
out = new FileOutputStream(filelocation);
byte[] buffer = new byte[1024];
int read;
while ((read = inputStream.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
out.flush();
inputStream.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
字符串filelocation是输出文件,它应该的位置保存。感谢大家 :)!
如果您的downloadLink有一个链接,您可以通过浏览器下载链接,它不仅打开与InputStream的链接并将内容保存在SD卡中? – jonathanrz