[英]How to create a folder and a file on Google Drive using API? Android
[英]How to Create a Folder in App Folder in google drive in Android Programmatically
我正在使用Android中的Google Drive API将数据存储在Google Drive中。 我提到了这个github 。 但这是为了在根文件夹而不是App文件夹中创建一个文件夹。 是否可以在AppFolder
创建文件夹? 我尝试了一些但没有结果,但我尝试过的代码。
public class CreateFolderInAppFolder extends BaseDemoActivity {
@Override
public void onConnected(Bundle connectionHint) {
super.onConnected(connectionHint);
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle("New folder").build();
DriveApi.getAppFolder(getGoogleApiClient()).createFolder(
getGoogleApiClient(), changeSet).setResultCallback(callback);
}
final ResultCallback<DriveFolderResult> callback = new ResultCallback<DriveFolderResult>() {
@Override
public void onResult(DriveFolderResult result) {
if (!result.getStatus().isSuccess()) {
showMessage("Error while trying to create the folder");
return;
}
showMessage("Created a folder: " + result.getDriveFolder().getDriveId());
}
};
}
是的,可以,尝试阅读存储应用程序数据 :
“应用程序文件夹”是一个特殊的文件夹 ,只能由您的应用程序访问。 对用户和其他应用程序隐藏其内容。 尽管对用户隐藏了该应用文件夹,但该应用文件夹存储在用户的云端硬盘中,因此会使用该用户的云端硬盘存储配额。 “应用程序文件夹”可用于存储配置文件,临时文件或属于用户但不得篡改的任何其他类型的文件。
在“应用程序文件夹”中创建文件与在普通文件夹中 创建文件相同。 下面的示例演示在“应用程序文件夹”中创建一个名为
appconfig.txt
的新文本文件:
这是一个示例代码:
final private ResultCallback<DriveContentsResult> contentsCallback =
new ResultCallback<DriveContentsResult>() {
@Override
public void onResult(DriveContentsResult result) {
if (!result.getStatus().isSuccess()) {
showMessage("Error while trying to create new file contents");
return;
}
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle("appconfig.txt")
.setMimeType("text/plain")
.build();
Drive.DriveApi.getAppFolder(getGoogleApiClient())
.createFile(getGoogleApiClient(), changeSet, result.getDriveContents())
.setResultCallback(fileCallback);
}
};
您可以通过获取元数据来检查文件/文件夹是否在AppFolder中。 如果文件或文件夹位于“应用程序文件夹”中,则Metadata.isInAppFolder
方法将返回true
。
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.