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..after,我想創建該文件夾中的文件folderId但當我使用通過該文件夾ID創建文件裏面它比說 「找不到DriveId。您有權限查看此文件嗎?」不能得到,我只是谷歌創建驅動
如果我使用result.getDriveFolder().getDriveId().getResourceId()
以獲得比它總是空的id。
我使用此代碼首先創建文件夾,我貼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
我保存和傳遞它來創建文件功能。
非常感謝你幫助了很多。 – 2015-03-16 17:25:31