6

我想將Google Drive與我的應用程序集成我已經在Google Developers Console中註冊了我的應用程序。我從https://github.com/googledrive/android-demos得到了一個樣本。通過這個我可以在Google驅動器的根文件夾中創建一個文件,但問題是我無法在現有文件夾中創建文件或文件夾。在這種情況下,我得到了一杯烤麪包「找不到DriveId,您有權查看此文件嗎?」即我無法獲得驅動器ID在Google Drive中創建文件夾內的文件夾android

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幫助我在哪裏出錯或是否需要包含任何其他權限

回答

1

什麼是EXISTING_FOLDER_ID?如果您試圖直接運行樣本而不進行任何更改,則此操作無效。

您需要將EXISTING_FOLDER_ID更改爲應用有權訪問的文件夾的資源ID。這可能是您的應用程序在根中創建的文件夾。

+0

是的,我所做的EXISTING_FOLDER_ID動態與驅動器Id的文件夾的值,我創造,但我得到的時候idCallback函數被調用 –

4

您可以在這裏看一下:在'createTree()'方法中,文件夾內創建了一個文件夾。

有新的谷歌雲端硬盤Android API 3個不同的驅動器ID實體(GDAA)

  1. 型驅動器Id的對象 - 你的方法獲取和在你的代碼
  2. 一個字符串使用一個從encodeToString得到(),並傳遞給decodeFromString() - 用於在應用程序中保存(緩存實例)
  3. 字符串你從getResourceId()獲取和傳遞到fetchDriveId() - 您在文件的html地址中看到的那個。

2和3標識符都是字符串,所以它們可能會混淆。標識符2在檢索驅動器ID時(通過decodeFromString())速度更快。標識符3檢索速度較慢(通過fetchDriveId()),但如果您需要將其他標識符(例如Apps Script)用於其他標識符,則該標識符有用。

另請參閱:SO 21800257

+0

我可以說__public靜態最後絃樂EXISTING_FOLDER_ID =網絡錯誤」 CAESABjACiCShczj8lI =「; __ – Nabin

+0

我在創建文件夾時複製了該文件夾的ID – Nabin

+0

@seanpj鏈接已損壞。 – nasch

0

首先使用creatreeTree文件夾()

然後運行搜索查詢,以獲得創建的ID 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使用此代碼來創建文件夾在文件夾`公共靜態ParentReference insertFileIntoFolder(驅動器服務,字符串folderId, 字符串文件夾名稱)拋出IOException異常{

// Log.e( 「founddd」,ID); File fileMetadata = new File(); ();}};}};}}} fileImagePolumn(Collection.singletonList(new ParentReference().setId(folderId == null?「root」:folderId))); fileMetadata.setTitle(folderName); fileMetadata.setMimeType(「application/vnd.google-apps。文件夾「);

 File file = mGOOSvc.files().insert(fileMetadata).execute(); 

     System.out.println("Folder ID: " + file.getId()); 
     strChildFolder = file.getId(); 




    return null; 
}` 
相關問題