从 Google 驱动器下载所选文件
Download selected file from Google drive
我有所选文件的驱动器 ID,我可以使用
获取该文件的 Url
MetadataResult mdRslt;
DriveFile file;
file = Drive.DriveApi.getFile(mGoogleApiClient,driveId);
mdRslt = file.getMetadata(mGoogleApiClient).await();
if (mdRslt != null && mdRslt.getStatus().isSuccess()) {
link = mdRslt.getMetadata().getWebContentLink();
if(link==null){
link = mdRslt.getMetadata().getAlternateLink();
Log.e("LINK","FILE URL After Null: "+ link);
}
Log.e("LINK","FILE URL : "+ link);
}
如何从url下载文件并保存到SD卡?请帮我解决这个问题。
谢谢
使用 downloadUrl 或 exportLinks 之一。有关这些属性的说明,请参阅 https://developers.google.com/drive/v2/reference/files。
更新:
实际上,由于您将其写入文件,因此不需要 'is2Bytes()'。只需将输入流 (cont.getInputStream()) 直接转储到文件即可。
原始答案:
由于您指的是 GDAA, this method (taken from here) 可能只适合您:
GoogleApiClient mGAC;
byte[] read(DriveId id) {
byte[] buf = null;
if (mGAC != null && mGAC.isConnected() && id != null) try {
DriveFile df = Drive.DriveApi.getFile(mGAC, id);
DriveContentsResult rslt = df.open(mGAC, DriveFile.MODE_READ_ONLY, null).await();
if ((rslt != null) && rslt.getStatus().isSuccess()) {
DriveContents cont = rslt.getDriveContents();
buf = is2Bytes(cont.getInputStream());
cont.discard(mGAC); // or cont.commit(); they are equiv if READONLY
}
} catch (Exception e) { Log.e("_", Log.getStackTraceString(e)); }
return buf;
}
byte[] is2Bytes(InputStream is) {
byte[] buf = null;
BufferedInputStream bufIS = null;
if (is != null) try {
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
bufIS = new BufferedInputStream(is);
buf = new byte[4096];
int cnt;
while ((cnt = bufIS.read(buf)) >= 0) {
byteBuffer.write(buf, 0, cnt);
}
buf = byteBuffer.size() > 0 ? byteBuffer.toByteArray() : null;
} catch (Exception ignore) {}
finally {
try {
if (bufIS != null) bufIS.close();
} catch (Exception ignore) {}
}
return buf;
}
它是 'await' 风格的简化版本,已 运行 关闭-UI-线程。另外,将输入流转储到缓冲区是可选的,我不知道你的是什么
应用程序的需求是。
祝你好运。
我有所选文件的驱动器 ID,我可以使用
获取该文件的 UrlMetadataResult mdRslt;
DriveFile file;
file = Drive.DriveApi.getFile(mGoogleApiClient,driveId);
mdRslt = file.getMetadata(mGoogleApiClient).await();
if (mdRslt != null && mdRslt.getStatus().isSuccess()) {
link = mdRslt.getMetadata().getWebContentLink();
if(link==null){
link = mdRslt.getMetadata().getAlternateLink();
Log.e("LINK","FILE URL After Null: "+ link);
}
Log.e("LINK","FILE URL : "+ link);
}
如何从url下载文件并保存到SD卡?请帮我解决这个问题。 谢谢
使用 downloadUrl 或 exportLinks 之一。有关这些属性的说明,请参阅 https://developers.google.com/drive/v2/reference/files。
更新:
实际上,由于您将其写入文件,因此不需要 'is2Bytes()'。只需将输入流 (cont.getInputStream()) 直接转储到文件即可。
原始答案:
由于您指的是 GDAA, this method (taken from here) 可能只适合您:
GoogleApiClient mGAC;
byte[] read(DriveId id) {
byte[] buf = null;
if (mGAC != null && mGAC.isConnected() && id != null) try {
DriveFile df = Drive.DriveApi.getFile(mGAC, id);
DriveContentsResult rslt = df.open(mGAC, DriveFile.MODE_READ_ONLY, null).await();
if ((rslt != null) && rslt.getStatus().isSuccess()) {
DriveContents cont = rslt.getDriveContents();
buf = is2Bytes(cont.getInputStream());
cont.discard(mGAC); // or cont.commit(); they are equiv if READONLY
}
} catch (Exception e) { Log.e("_", Log.getStackTraceString(e)); }
return buf;
}
byte[] is2Bytes(InputStream is) {
byte[] buf = null;
BufferedInputStream bufIS = null;
if (is != null) try {
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
bufIS = new BufferedInputStream(is);
buf = new byte[4096];
int cnt;
while ((cnt = bufIS.read(buf)) >= 0) {
byteBuffer.write(buf, 0, cnt);
}
buf = byteBuffer.size() > 0 ? byteBuffer.toByteArray() : null;
} catch (Exception ignore) {}
finally {
try {
if (bufIS != null) bufIS.close();
} catch (Exception ignore) {}
}
return buf;
}
它是 'await' 风格的简化版本,已 运行 关闭-UI-线程。另外,将输入流转储到缓冲区是可选的,我不知道你的是什么 应用程序的需求是。
祝你好运。