【发布时间】:2014-02-05 06:09:59
【问题描述】:
我花了一天多的时间,但没有得到任何可行的解决方案,可以让我将文件上传/下载到Google Drive。
我尝试了Google Play Service,但我没有找到任何上传/下载文件的方法。
我尝试了谷歌客户端库,但有一些方法没有解决。
如:
service.files().insert(body, mediaContent).execute();
errors: The method execute() is undefined for the type Drive.Files.Insert
我可以通过以下代码上传图片,但这是 Google Drive 文件上传器。我一次只能上传一个文件。
mFile = new java.io.File(fileList.get(i));
Log.i(TAG, "Creating new contents.");
Drive.DriveApi.newContents(mGoogleApiClient).addResultCallback(
new OnNewContentsCallback() {
@Override
public void onNewContents(ContentsResult result) {
if (!result.getStatus().isSuccess()) {
Log.i(TAG, "Failed to create new contents.");
return;
}
Log.i(TAG, "New contents created.");
OutputStream outputStream = result
.getContents().getOutputStream();
byte[] byteStream = new byte[(int) mFile
.length()];
try {
outputStream.write(byteStream);
} catch (IOException e1) {
Log.i(TAG, "Unable to write file contents.");
}
MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
.setMimeType("image/jpeg")
.setMimeType("text/html")
.setTitle("Android Photo.png").build();
// Create an intent for the file chooser, and
// start it.
IntentSender intentSender = Drive.DriveApi
.newCreateFileActivityBuilder()
.setInitialMetadata(metadataChangeSet)
.setInitialContents(
result.getContents())
.build(mGoogleApiClient);
try {
mActivity.startIntentSenderForResult(
intentSender, REQUEST_CODE_CREATOR,
null, 0, 0, 0);
publishProgress(1);
} catch (SendIntentException e) {
Log.i(TAG, "Failed to launch file chooser.");
publishProgress(0);
}
}
});
但仍在为下载文件而战。
【问题讨论】:
-
嗨,MD,这与代码无关。实际上问题是我应该使用 Google Play 服务或 Google 提供的客户端库。如果我使用库,那么我们不能使用 Google Play 服务,因为某些文件被交叉,例如 Drive 类。它在图书馆和 Google Play 服务中。如果我使用的是 Google Play 服务,那么我不知道任何上传下载方法。
-
感谢@Praveen,但如果未安装驱动应用程序会发生什么。我正在将 Google Drive 合并到我的应用中。实际上我正在合并 Dropbox、Google Drive、SkyDrive、Amazon 等。
-
@MD,我认为我们不应该同时使用库 Google Play 服务和 Google 客户端库,还是应该使用?
-
@PKTomar 你绝对正确。我遇到了同样的问题