无法获得我刚刚在谷歌硬盘上创建的folderId

时间:2015-03-13 10:39:01

标签: android google-drive-api google-play-services google-drive-android-api

public class Helpers  extends Activity implements
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener {
SharedPreferences preferences;

private static final String TAG = "BaseDriveActivity";

public static  String EXISTING_FOLDER_ID;


protected static final int REQUEST_CODE_RESOLUTION = 1;

protected static final int NEXT_AVAILABLE_REQUEST_CODE = 2;

public static final String folderId = "FOLDER_ID";
public static final String fileName = "folderId";

private GoogleApiClient mGoogleApiClient;


@Override
protected void onResume() {
    super.onResume();
    if (mGoogleApiClient == null) {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE)
                .addScope(Drive.SCOPE_APPFOLDER) // required for App Folder sample
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
    }
    mGoogleApiClient.connect();
}

@Override
protected void onActivityResult(int requestCode, int resultCode,
                                Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_CODE_RESOLUTION && resultCode == RESULT_OK) {
        mGoogleApiClient.connect();
    }
}

@Override
protected void onPause() {
    if (mGoogleApiClient != null) {
        mGoogleApiClient.disconnect();
    }
    super.onPause();
}

final ResultCallback<DriveFolder.DriveFolderResult> callback = new ResultCallback<DriveFolder.DriveFolderResult>() {
    @Override
    public void onResult(DriveFolder.DriveFolderResult result) {
        if (!result.getStatus().isSuccess()) {
            showMessage("Error while trying to create the folder");
            return;
        }

        EXISTING_FOLDER_ID = result.getDriveFolder().getDriveId().toString();
        showMessage("Created a folder: " + result.getDriveFolder().getDriveId().toString());

        preferences = getSharedPreferences(fileName , MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(folderId , EXISTING_FOLDER_ID );
        editor.apply();
    }
};

public  void mcreateFolder() {

    MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
            .setTitle("SmsSync").build();
    Drive.DriveApi.getRootFolder(getGoogleApiClient()).createFolder(
            getGoogleApiClient(), changeSet).setResultCallback(callback);

}

@Override
public void onConnected(Bundle bundle) {
    Log.i(TAG, "GoogleApiClient connected");
    preferences = getSharedPreferences(fileName , MODE_PRIVATE);
    EXISTING_FOLDER_ID = preferences.getString(folderId ,null);
    if (EXISTING_FOLDER_ID == null ) {
        mcreateFolder();

    }

}

@Override
public void onConnectionSuspended(int cause) {
    Log.i(TAG, "GoogleApiClient connection suspended");
}

@Override
public void onConnectionFailed(ConnectionResult result) {
    Log.i(TAG, "GoogleApiClient connection failed: " + result.toString());
    if (!result.hasResolution()) {
        // show the localized error dialog.
        GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this, 0).show();
        return;
    }
    try {
        result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
    } catch (IntentSender.SendIntentException e) {
        Log.e(TAG, "Exception while starting resolution activity", e);
    }
}

public void showMessage(String message) {
    Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}

public GoogleApiClient getGoogleApiClient() {
    return mGoogleApiClient;
}}

我想在安装应用程序时创建一个文件夹,然后获取其ID并将其保存到sharedprefrences ..之后我想在该文件夹中创建一个文件,但是当我使用传递该文件夹ID来创建其中的文件时说 &#34;无法找到DriveId。您是否有权查看此文件?&#34;

如果我使用result.getDriveFolder().getDriveId().getResourceId() 得到id而不是它总是null 我首先使用此代码创建文件夹,我粘贴https://github.com/googledrive/android-demos/blob/master/src/com/google/android/gms/drive/sample/demo/CreateFolderActivity.java

并在我正在使用本指南的文件夹中创建文件 https://github.com/googledrive/android-demos/blob/master/src/com/google/android/gms/drive/sample/demo/CreateFileInFolderActivity.java
我传递了我保存的folderiD并将其传递给创建文件函数。

1 个答案:

答案 0 :(得分:8)

不要将DriveId与ResourceId混合使用。两者看起来都像字符串,但DriveId与ResourceId不同。见SO 21800257。此外,ResourceId无法立即使用,请参阅SO 22874657

DriveId通常如下所示:

  

&#34;驱动器Id:CAESHDBCMW1RVblahblahblahblahMYjAUgssy8yYFRTTNKRU55&#34;

而ResourceId更像是:

  

&#34; UW2ablahblaghblahNy00Ums0B1mQ&#34;

<强>更新

由于有这么多开发人员在讨论这个问题,我会尽量详细说明我的知识。

     Google Drive             Google Play Svcs      YourApp
   (up in the cloud)         (on your device)      (on your device)
  +--------------------+      +--------------+     +--------------+         
  | 'id' (RESTful API) | - - -> ResourceId  - - - -> ResourceId   |
  +--------------------+      |  DriveId    - - - -> DriveId      |
                              +--------------+     +--------------+

我试图用上面的艺术表达来表达的是:

  • 当您在设备上创建驱动器对象(文件夹/文件)时,GooPlaySvcs会为您提供DriveId
  • 您可以使用此DriveId与GooPlaySvcs进行本地通信,您可以对其进行缓存等。
  • Per Daniel在SO 21800257(上面的链接)中的评论,不要依赖DriveId作为常量字符串, 据推测它会改变被提交的对象。使用DriveId.equals()(我没有测试)
  • 任何时候你走出本地设备(驱动器网络界面,其他应用程序,YourApp在另一个 设备),您需要使用ResourceId,这是驱动器上唯一的唯一ID(在云中: - )。
  • 您的AFTER GooPlaySvcs可以使用ResourceId将对象提交到云端硬盘。有 强制它的方法,但它是一个不同的故事(搜索requestSync())。
  • 如果您决定获取ResourceId并将其用于RESTfull调用(delete/trash),请注意 Google Play服务按照您无法控制的时间表传播其更改的事实 结束(所以看起来再次看到requestSync()问题),你的REST / GDAA战斗可能会造成损害 你的数据。 GDAA(GooPlayServices)可能暂时不知道您的REST更改。你有 自己管理同步。我承认,当我尝试的时候,我失败了。

    祝你好运