我正在创建一个应用程序,该应用程序应该使用相关API将文件保存到Google云端硬盘。我已管理登录,现在我希望该应用在Google云端硬盘中创建一个文件夹,然后将该文件写入该文件夹。
由于Drive API是异步的,我试图用continueWithTask()连接几个任务。在以下代码中对continueWithTask()的最后一次调用引发了Android Studio错误消息:" continueWithTask(... Continuation< ... DriveFolder,... Task< ... DriveFile>>)在Task中无法应用于(匿名...延续< ... DriveContents,...任务< ... DriveFile>>)"。
我不知道为什么" DriveFolder"出现在上面的错误消息中,因为在最后一个continueWithTask()参数中没有使用DriveFolder。唯一的DriveFolder是m_DriveFolder类成员。
有人可以帮我理解错误的位置吗?
void SyncToDrive()
{
Task<DriveFolder> l_TaskSyncToDrive;
// Gets the resource client
m_DriveResourceClient = Drive.getDriveResourceClient(this, m_GoogleSignInAccount);
//Creates the new folder
l_TaskSyncToDrive =
m_DriveResourceClient
.getRootFolder()
.continueWithTask(new Continuation<DriveFolder, Task<DriveFolder>>()
{
@Override
public Task<DriveFolder> then(@NonNull Task<DriveFolder> task) throws Exception
{
DriveFolder parentFolder = task.getResult();
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle("MyFolder")
.setMimeType(DriveFolder.MIME_TYPE)
.setStarred(true)
.build();
return m_DriveResourceClient.createFolder(parentFolder, changeSet);
}
})
.addOnSuccessListener(this,
new OnSuccessListener<DriveFolder>()
{
@Override
public void onSuccess(DriveFolder driveFolder)
{
Toast toast = Toast.makeText(getApplicationContext(), driveFolder.getDriveId().encodeToString(),
Toast.LENGTH_SHORT);
toast.show();
}
})
.addOnFailureListener(this,
new OnFailureListener()
{
@Override
public void onFailure(@NonNull Exception e)
{
Log.e("D103", "Unable to create folder", e);
Toast toast = Toast.makeText(getApplicationContext(), e.getMessage(),
Toast.LENGTH_SHORT);
toast.show();
}
});
// Creates the contents
l_TaskSyncToDrive.continueWithTask(new Continuation<DriveFolder, Task<DriveContents>>()
{
@Override
public Task<DriveContents> then(@NonNull Task<DriveFolder> p_DriveFolder) throws Exception
{
m_DriveFolder = p_DriveFolder.getResult();
return m_DriveResourceClient.createContents();
}
});
// Creates the file
l_TaskSyncToDrive.continueWithTask(new Continuation<DriveContents, Task<DriveFile>>()
{
@Override
public Task<DriveFile> then(@NonNull Task<DriveContents> p_DriveContents) throws Exception
{
DriveContents l_DriveContents;
OutputStream l_OutputStream;
DataOutputStream l_DataOutputStream;
l_DriveContents = p_DriveContents.getResult();
l_OutputStream = l_DriveContents.getOutputStream();
l_DataOutputStream = new DataOutputStream(l_OutputStream);
if(!(SafeApplication.m_FileManager.ExportFile(l_DataOutputStream, SafeApplication.m_Elements)))
{
// TODO handle error
}
MetadataChangeSet l_ChangeSet = new MetadataChangeSet.Builder()
.setTitle("MyFileName")
.setMimeType("application/octet-stream")
.build();
return m_DriveResourceClient.createFile(m_DriveFolder, l_ChangeSet, l_DriveContents);
}
})
.addOnFailureListener(new OnFailureListener()
{
@Override
public void onFailure(@NonNull Exception e)
{
Log.w("D102", "Failed to create file.", e);
Toast toast = Toast.makeText(getApplicationContext(), e.getMessage(),
Toast.LENGTH_SHORT);
toast.show();
}
});
}