[英]Pick any file from users Google Drive and send (Upload) it to my server via Android app
I am using the Google Drive Android API (GDAA) and I want to enable the user to pick any file (Google Docs, Sheets, Images, Video, Audio etc.) from their Drive and send it to the app server. 我正在使用Google Drive Android API(GDAA),并且希望使用户能够从其Drive中选择任何文件(Google文档,表格,图像,视频,音频等)并将其发送到应用服务器。
I have followed several tutorials on the web and also the official Google documentation for using the GDAA. 我已经在网上关注了一些教程以及有关使用GDAA的Google官方文档。
I tried the following answer as well: Fetching contents of Google Drive file through FileId
我也尝试了以下答案: 通过
FileId
获取Google云端硬盘文件的内容
But this too does not work. 但这也不起作用。 I get
result.getStatus().isSuccess()
as false
always. 我总是将
result.getStatus().isSuccess()
设为false
。 I do not understand why. 我不理解为什么。
The following is my open()
function: 以下是我的
open()
函数:
private void open() {
DriveFile driveFile = mFileId.asDriveFile();
driveFile.open(mGoogleApiClient, DriveFile.MODE_READ_ONLY, null)
.setResultCallback(driveContentsCallback);
mFileId = null;
}
And the following is the callback where I get result.getStatus().isSuccess()
as false
: 以下是我得到
result.getStatus().isSuccess()
为false
的回调:
/**
* This is Result result handler of Drive contents.
*/
final ResultCallback<DriveApi.DriveContentsResult> driveContentsCallback =
new ResultCallback<DriveApi.DriveContentsResult>() {
@Override
public void onResult(DriveApi.DriveContentsResult result) {
if (!result.getStatus().isSuccess()) {
return;
}
// Read from the input stream an print to LOGCAT
DriveContents driveContents = result.getDriveContents();
BufferedReader reader = new BufferedReader(new InputStreamReader(driveContents.getInputStream()));
StringBuilder builder = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
String contentsAsString = builder.toString();
Log.i("ContentAsString", contentsAsString);
// Close file contents
driveContents.discard(mGoogleApiClient);
}
};
For now I am relying on this code. 现在,我依靠此代码。 I have been able to open Google Sheets and Docs but I need to upload them as well as images and videos to my app server (Opening the file is not necessary).
我已经能够打开Google表格和文档,但是我需要将它们以及图像和视频上传到我的应用服务器(无需打开文件)。 Is there a way I can achieve this?
有没有办法可以做到这一点?
I have read about the Storage Access Framework (SAF) and it may allow me to access the Google Drive files Kitkat onward but my app supports devices running on JellyBean and upwards and thus I am going with the GGDA. 我已经阅读了有关Storage Access Framework(SAF)的信息,它可能允许我从以后访问Kitkat的Google云端硬盘文件,但是我的应用支持在JellyBean以及更高版本上运行的设备,因此我要使用GGDA。
I have not checked the REST API yet. 我尚未检查REST API。 I want to understand if I am doing this the right way or is there a totally different way to achieve what I want.
我想了解我是否以正确的方式进行操作,或者是否有完全不同的方式来实现自己想要的目标。 And if this is the right way, what is going wrong?
如果这是正确的方法,那么哪里出了问题?
Any help will be appreciated. 任何帮助将不胜感激。
You may try the following workaround: 您可以尝试以下解决方法:
google-services.json
in your app folder is correct. google-services.json
是正确的。 string.xml
matches the one in your Google Developer Console. string.xml
中的API密钥与Google Developer Console中的API密钥相匹配。 If these don't help, try to create a new API key based on a new project in Google Developer Console. 如果这些方法没有帮助,请尝试在Google Developer Console中基于新项目创建新的API密钥。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.