我具有所选文件的驱动器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卡?请帮助我。谢谢。
更新:
实际上,由于您将其写入文件,因此不需要'is2Bytes()'。只需将输入流(cont.getInputStream())直接转储到文件中即可。
原始答案:
由于您是指GDAA,因此此方法(从此处获取)可能对您有效:
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;
}
它是“等待”风格的简化版本,已在UI线程外运行。另外,将输入流转储到缓冲区是可选的,我不知道您的应用程序需要什么。
祝你好运。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句