问题
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)
回答1:
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
来源:https://stackoverflow.com/questions/29050204/cannot-access-google-drive-from-2-different-device