使用 google-api-services-drive:v3 从 google 驱动器下载文件
Download file from google drive using google-api-services-drive:v3
我只是无法从我刚刚创建的 google 驱动器下载文件。我可以获得 webContentLink。如果我通过浏览器访问它,我可以下载文件,但我不能通过我的应用程序下载相同的文件...我几乎阅读了所有关于 google 驱动器的文档,但我找不到任何下载文件的方法到我的存储空间。
在这里,我得到了文件的 link...我可以像这样得到我想要的任何东西
downloadLink = m.getAlternateLink();
downloadLink = m.getEmbedLink();
downloadLink = m.getWebContentLink();
downloadLink = m.getWebViewLink();
或其他所有参数,例如 DriveId...
这是我的代码,用于获取驱动器上文件的信息:
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();
}
}
};
String filelocation 是输出文件的位置,它应该保存在哪里。谢谢大家:)!
驱动器 API 有一个 full guide on working with file contents,即获取文件的直接 InputStream
。
给定一个DriveFile
,你用
打开它
DriveFile file = ...
file.open(mGoogleApiClient, DriveFile.MODE_READ_ONLY, null)
.setResultCallback(contentsOpenedCallback);
使用 ResultCallback
看起来像:
ResultCallback<DriveContentsResult> contentsOpenedCallback =
new ResultCallback<DriveContentsResult>() {
@Override
public void onResult(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();
}
};
然后您可以通过 contents.getInputStream()
从 InputStream
中读取,如果您愿意,可以将其写入本地文件。
请记住,由于 Google Drive 已经在您打开文件后保留了一份本地副本,如果您只需要应用程序中的内容,它最好直接使用 InputStream
而不是将其保存到另一个本地文件(需要加倍存储 space)。
我只是无法从我刚刚创建的 google 驱动器下载文件。我可以获得 webContentLink。如果我通过浏览器访问它,我可以下载文件,但我不能通过我的应用程序下载相同的文件...我几乎阅读了所有关于 google 驱动器的文档,但我找不到任何下载文件的方法到我的存储空间。
在这里,我得到了文件的 link...我可以像这样得到我想要的任何东西
downloadLink = m.getAlternateLink();
downloadLink = m.getEmbedLink();
downloadLink = m.getWebContentLink();
downloadLink = m.getWebViewLink();
或其他所有参数,例如 DriveId...
这是我的代码,用于获取驱动器上文件的信息:
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();
}
}
};
String filelocation 是输出文件的位置,它应该保存在哪里。谢谢大家:)!
驱动器 API 有一个 full guide on working with file contents,即获取文件的直接 InputStream
。
给定一个DriveFile
,你用
DriveFile file = ...
file.open(mGoogleApiClient, DriveFile.MODE_READ_ONLY, null)
.setResultCallback(contentsOpenedCallback);
使用 ResultCallback
看起来像:
ResultCallback<DriveContentsResult> contentsOpenedCallback =
new ResultCallback<DriveContentsResult>() {
@Override
public void onResult(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();
}
};
然后您可以通过 contents.getInputStream()
从 InputStream
中读取,如果您愿意,可以将其写入本地文件。
请记住,由于 Google Drive 已经在您打开文件后保留了一份本地副本,如果您只需要应用程序中的内容,它最好直接使用 InputStream
而不是将其保存到另一个本地文件(需要加倍存储 space)。