我想将Google驱动器与我在Google Developers Console中注册我的应用的应用集成。我从https://github.com/googledrive/android-demos获得了一个示例。通过这个我可以在Google驱动器的根文件夹中创建一个文件,但问题是我无法在现有文件夹中创建文件或文件夹。在这种情况下,我得到一个祝酒词“找不到DriveId。您是否有权查看此文件?”即我无法获得driveID
public class CreateFolderInFolderActivity extends BaseDemoActivity {
@Override
public void onConnected(Bundle connectionHint) {
super.onConnected(connectionHint);
Drive.DriveApi.fetchDriveId(getGoogleApiClient(), EXISTING_FOLDER_ID)
.setResultCallback(idCallback);
}
final ResultCallback<DriveIdResult> idCallback = new ResultCallback<DriveIdResult>() {
@Override
public void onResult(DriveIdResult result) {
if (!result.getStatus().isSuccess()) {
showMessage(result.getStatus().toString());
showMessage("Cannot find DriveId. Are you authorized to view this file?");
return;
}
DriveFolder folder = Drive.DriveApi
.getFolder(getGoogleApiClient(), result.getDriveId());
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle("MyNewFolder").build();
folder.createFolder(getGoogleApiClient(), changeSet)
.setResultCallback(createFolderCallback);
}
};
final ResultCallback<DriveFolderResult> createFolderCallback = new
ResultCallback<DriveFolderResult>() {
@Override
public void onResult(DriveFolderResult result) {
if (!result.getStatus().isSuccess()) {
showMessage("Problem while trying to create a folder");
return;
}
showMessage("Folder successfully created");
}
};
}
我找不到任何适当的文件。 Plz帮助我在哪里出错或者我是否必须包含任何其他权限
答案 0 :(得分:4)
您可以在此处查看:在“createTree()”方法中,文件夹中创建了一个文件夹。
新版Google云端硬盘Android API(GDAA)中有3种不同的驱动器ID实体
2和3标识符都是字符串,因此可能会混淆。检索驱动器ID时,标识符2更快(通过decodeFromString())。标识符3的检索速度较慢(通过fetchDriveId()),但如果您需要在其他位置使用您的ID(例如,应用程序脚本),则会很有用。
另请参阅:SO 21800257
答案 1 :(得分:1)
什么是EXISTING_FOLDER_ID?如果您试图直接运行样本而不进行任何更改,这将无法正常工作。
您需要将EXISTING_FOLDER_ID更改为您的应用有权访问的文件夹的资源ID。这可能是您的应用在根目录中创建的文件夹。
答案 2 :(得分:0)
首先使用creatreeTree()
创建文件夹然后运行搜索查询以获取create public static ArrayList<ContentValues> search(String prnId, String titl, String mime) {
ArrayList<ContentValues> gfs = new ArrayList<>();
if (mGOOSvc != null && mConnected) try {
// add query conditions, build query
String qryClause = "'me' in owners and ";
if (prnId != null) qryClause += "'" + prnId + "' in parents and ";
if (titl != null) qryClause += "title = '" + titl + "' and ";
if (mime != null) qryClause += "mimeType = '" + mime + "' and ";
qryClause = qryClause.substring(0, qryClause.length() - " and ".length());
Drive.Files.List qry = mGOOSvc.files().list().setQ(qryClause)
.setFields("items(id,mimeType,labels/trashed,title),nextPageToken");
String npTok = null;
if (qry != null) do {
FileList gLst = qry.execute();
if (gLst != null) {
for (File gFl : gLst.getItems()) {
if (gFl.getLabels().getTrashed()) continue;
gfs.add( UT.newCVs(gFl.getTitle(), gFl.getId(), gFl.getMimeType()));
} //else UT.lg("failed " + gFl.getTitle());
npTok = gLst.getNextPageToken();
qry.setPageToken(npTok);
}
} while (npTok != null && npTok.length() > 0); //UT.lg("found " + vlss.size());
} catch (Exception e) { UT.le(e); }
return gfs;
}
获取文件夹ID时使用此代码在文件夹`public static ParentReference insertFileIntoFolder(Drive service,String folderId, String folderName)抛出IOException {
// Log.e(“founddd”,id); 文件fileMetadata = new File(); fileMetadata.setParents(Collections.singletonList(new ParentReference()。setId(folderId == null?“root”:folderId))); fileMetadata.setTitle(FOLDERNAME); fileMetadata.setMimeType( “应用程序/ vnd.google-apps.folder”);
File file = mGOOSvc.files().insert(fileMetadata).execute();
System.out.println("Folder ID: " + file.getId());
strChildFolder = file.getId();
return null;
}`