0

I have an app which creates a word file on the Google Drive with some text. If I try to access the word file on the Google Drive from the device In which I have created that file, then it works and everything thing goes smooth, and I can able to read and write. But If I try to access the same file with the same DriveId on the different device with the same app, then I can't access it. I have seen 2 related questions on the StackOverflow but I can't figure out the problem. Any help would be appreciated.

This is the GoogleApiClient Connection I am using:-

    if(mGoogleApiClient == null)
    {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Drive.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addScope(Drive.SCOPE_FILE)
                .build();
    }
    mGoogleApiClient.connect();

This is the program code:-

@Override
public void onConnected(Bundle bundle) {
    msg.Log("onConnected");

            msg.Log(inputText.encodeToString());
            DriveFile file = Drive.DriveApi.getFile(mGoogleApiClient, inputText);
            file.open(mGoogleApiClient, DriveFile.MODE_READ_ONLY, null).setResultCallback(contentsOpenedCallBack);
}

final private ResultCallback<DriveApi.DriveContentsResult> contentsOpenedCallBack = new ResultCallback<DriveApi.DriveContentsResult>() {
    @Override
    public void onResult(DriveApi.DriveContentsResult driveContentsResult) {

        msg.Log("onResult");
        msg.Log("toString ");
        if (!driveContentsResult.getStatus().isSuccess()) {
            Toast.makeText(getBaseContext(), "Wrong Success", Toast.LENGTH_SHORT).show();

        }

            final DriveContents contents = driveContentsResult.getDriveContents();


                    BufferedReader reader = new BufferedReader(new InputStreamReader(contents.getInputStream()));
                    StringBuilder builder = new StringBuilder();
                    String line;

                    try {
                        while ((line = reader.readLine()) != null) {
                            builder.append(line);
                        }
                    } catch (IOException e) {
                        msg.Log(e.toString());
                    }

                    String contentsAsString = builder.toString();

                    try {
                        JSONObject json = new JSONObject(contentsAsString);

                        String Text = json.getString("text");


                        SharedPreferences sp = getSharedPreferences("Font", Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putString("value", Text);
                        msg.Log(Password);


                    } catch (Exception e) {
                        msg.Log(e.toString());
                    }

        }

};

This is the error message:-

java.lang.SecurityException: Permission Denial: get/set setting for user asks to run as user -2 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL
        at com.android.server.am.ActivityManagerService.handleIncomingUser(ActivityManagerService.java:13140)
        at android.app.ActivityManager.handleIncomingUser(ActivityManager.java:2038)
        at com.android.providers.settings.SettingsProvider.callFromPackage(SettingsProvider.java:607)
        at android.content.ContentProvider$Transport.call(ContentProvider.java:279)
        at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:273)
        at android.os.Binder.execTransact(Binder.java:388)
        at dalvik.system.NativeStart.run(Native Method)
03-14 21:48:48.885    2341-3805/? E/EnterpriseContainerManager﹕ ContainerPolicy Service is not yet ready!!!
03-14 21:48:48.895  25947-25947/com.gajendraprofile.googledrivesample E/ViewRootImpl﹕ sendUserActionEvent() mView == null
03-14 21:48:50.615   9473-26051/? E/DriveAsyncService﹕ Provided DriveId is invalid.
OperationException[Status{statusCode=Provided DriveId is invalid., resolution=null}]
        at com.google.android.gms.drive.api.e.d(SourceFile:331)
        at com.google.android.gms.drive.api.e.a(SourceFile:632)
        at com.google.android.gms.drive.api.a.ab.a(SourceFile:85)
        at com.google.android.gms.drive.api.a.b.a(SourceFile:27)
        at com.google.android.gms.common.service.c.onHandleIntent(SourceFile:60)
        at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:176)
        at android.os.HandlerThread.run(HandlerThread.java:61)
Gaj
  • 150
  • 2
  • 15
  • Check that the ID you are using is valid by using https://developers.google.com/drive/v2/reference/files/get#try-it. Check that project ID is the same since you are using drive.file scope. Are you confusing the ID returned by GDAA with the ID used by the REST API, coz they're different. – pinoyyid Mar 14 '15 at 15:16
  • @pinoyyid Thanks for your response. Actually I have 2 classes on my app, called `Activate` and `Validate`. `Activate` class creates a file and shows the File `DriveId` to the user. And when the user click `Validate` class. A Dialog box will ask for the File `DriveId`. After Taking the file `DriveId`, the app will read the data from the file, which was on the Google Drive. This process can be done on the device in which the file was created. But when I try to access `Validate` class on the other device using the File `DriveId`. Then I am unable to access. What could be the problem? – Gaj Mar 14 '15 at 15:50
  • @pinoyyid I've checked the drive id whether it is valid and also checked the project Id. Still the problem is not resolved. Totally stuck. Any Suggestions appreciated. Thanks – Gaj Mar 14 '15 at 20:04
  • Read the answer to this http://stackoverflow.com/questions/29030110/cannot-get-folderid-that-i-just-created-on-google-drive to ensure you are using the correct ID – pinoyyid Mar 14 '15 at 21:32
  • @pinoyyid I am 100% sure that the drive id is correct. Because when try from the device in which the file was created. It perfectly works. But it is not working on the alternative device. Please have a look at the stack above. I have updated them. Thanks – Gaj Mar 14 '15 at 21:52
  • See seanpj's answer, read the link I sent, and check you are using the correct ID. – pinoyyid Mar 15 '15 at 04:29

1 Answers1

2

DriveId means nothing on a different device. If you want to work with a file/folder, on a different device you have to use the ResourceId. That's the old 'id' from RESTful API. DriveId is specific to one particular instance of Google Play Services*.

You can also search for files/folders using metadata like title, mime, parent, ... but! Only DriveId and ResourceId are unique IDs, everything else (including title) is not. That means you can have multiple files / folders of the same name in the same location on the Drive !

*) There is an attempt to explain this on SO 29030110 .

Good Luck

Community
  • 1
  • 1
seanpj
  • 6,735
  • 2
  • 33
  • 54
  • If i want to get the data from the `Google Drive` using the resourceId (using the same app in the different device). Then do I need to use Drive REST API? similar to this example [link](https://developers.google.com/drive/v2/reference/files/get) ? – Gaj Mar 19 '15 at 10:29
  • The ResourceId will be the same on both devices. You can use GDAA. But still, you have to manage the latency / synchronization. As I was told once: "It will happen eventually". BTW, you'll see the ResourceId in web interface if you right click > share > get shareable link. – seanpj Mar 19 '15 at 15:05
  • If you use REST, the effects will be immediate (you still have to run it off-UI-thread and wait for http 'execute' request to finish). – seanpj Mar 19 '15 at 15:08
  • Ok Thanks, I will try as you said. – Gaj Mar 19 '15 at 15:16
  • DriveId method dint work for me aswell. I am trying to find a solution for this problem from past 3 days. But I cant find any answers. Please let me know if any of you found any alternative method. – Gaj Mar 19 '15 at 20:00