本文介绍了无法获取我刚刚在谷歌驱动器上创建的文件夹 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 在其中创建文件时,它比它说找不到 DriveId.您有权查看此文件吗?"

I want to create a folder when app is installed and than get its ID and save it to sharedprefrences..after that i wanted to create a file inside that folder but when i use pass that folderID to create file inside it than it says"Cannot find DriveId. Are you authorized to view this file?"

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

if I use result.getDriveFolder().getDriveId().getResourceId()to get id than its always null.
I am using this code to create folder first which I pasted 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 并将其传递给创建文件函数.

and to create a file inside a folder I am using this guidehttps://github.com/googledrive/android-demos/blob/master/src/com/google/android/gms/drive/sample/demo/CreateFileInFolderActivity.java
I am passing the folderiD that I save and passing it to create file function.

推荐答案

不要将 DriveId 与 ResourceId 混用.两者看起来都像字符串,但 DriveId 与 ResourceId 不同.请参阅 SO 21800257.此外,ResourceId 并非立即可用,请参阅 SO 22874657.

Do not mix DriveId with ResourceId. Both look like strings, but DriveId is different from ResourceId. See SO 21800257. Also, ResourceId is not immediately available, see SO 22874657.

DriveId 通常看起来像:

DriveId usually looks like:

"DriveId:CAESHDBCMW1RVblahblahblahblahMYjAUgssy8yYFRTTNKRU55"

而 ResourceId 更像:

whereas ResourceId is more like:

UW2ablahblaghblahNy00Ums0B1mQ"

更新:

由于有这么多开发人员在解决这个问题,我会尽量详细地阐述我的知识.

Since so many developers fight this issue, I'll try to elaborate as deep as my knowledge allows me to.

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

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

What I'm trying to convey with the artistic expression above is:

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

  • When you create a drive object (folder/file) on your device, GooPlaySvcs will give you the DriveId
  • You can use this DriveId for local communication with GooPlaySvcs, you can cache it, etc.
  • Per Daniel's comment in SO 21800257 (link above), do not rely on DriveId to be a constant string,it supposedly changes upon the object being committed. Use DriveId.equals() (I did not test that)
  • Anytime you step outside of the local device (Drive web interface, other apps, YourApp on a differentdevice), you need to use ResourceId, which is the only unique ID on the Drive (up in the cloud :-).
  • The ResourceId is available to your AFTER GooPlaySvcs commit the object to the Drive. There areways to force it, but it is a different story (search for requestSync()).
  • If you decide to grab the ResourceId and use it for RESTfull calls (delete/trash), be awareof the fact that Google Play Services propagates its changes on a schedule you don't have controlover (so it seems, see the requestSync() issue again), and your REST/GDAA fight can cause damageto your data. GDAA(GooPlayServices) may not be aware of your REST changes for a while. You haveto manage the synchronization yourself. I admit I failed miserably when I tried.

祝你好运

这篇关于无法获取我刚刚在谷歌驱动器上创建的文件夹 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 12:32