本文整理汇总了Java中com.google.android.gms.drive.DriveApi类的典型用法代码示例。如果您正苦于以下问题:Java DriveApi类的具体用法?Java DriveApi怎么用?Java DriveApi使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DriveApi类属于com.google.android.gms.drive包,在下文中一共展示了DriveApi类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: saveDriveFile
import com.google.android.gms.drive.DriveApi; //导入依赖的package包/类
/**
* Save the {@code DriveFile} with the specific driveId.
*
* @param id {@link DriveId} of the file.
* @param content The content to be saved in the {@code DriveFile}.
* @return Return value indicates whether the save was successful.
*/
public boolean saveDriveFile(DriveId id, String content) throws IOException {
DriveFile theFile = Drive.DriveApi.getFile(mGoogleApiClient, id);
DriveApi.DriveContentsResult result =
theFile.open(mGoogleApiClient, DriveFile.MODE_WRITE_ONLY, null).await();
try {
IOUtils.writeToStream(content, result.getDriveContents().getOutputStream());
// Update the last viewed.
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setLastViewedByMeDate(new Date())
.build();
return result.getDriveContents().commit(mGoogleApiClient, changeSet)
.await().isSuccess();
} catch (IOException io) {
result.getDriveContents().discard(mGoogleApiClient);
throw io;
}
}
示例2: onResult
import com.google.android.gms.drive.DriveApi; //导入依赖的package包/类
@Override
public void onResult(@NonNull DriveApi.DriveContentsResult driveContentsResult) {
try {
InputStream inputStream = driveContentsResult.getDriveContents().getInputStream();
byte[] b = new byte[4096];
String current = "";
int read = 0;
while ((read = inputStream.read(b, 0, 4096)) != -1)
current += new String(b, 0, read);
if (current.length() > 0)
DataManager.saveJsonAsData(new JSONObject(current));
driveContentsResult.getDriveContents().reopenForWrite(mGoogleApiClient).setResultCallback(writeDriveCallback);
} catch (IOException | JSONException e) {
Toast.makeText(getApplicationContext(), getString(R.string.drive_fail_get_data), Toast.LENGTH_LONG).show();
}
}
示例3: FindOrCreateDriveFile
import com.google.android.gms.drive.DriveApi; //导入依赖的package包/类
void FindOrCreateDriveFile(){
// Check if we already have a driveID file
Context context = getApplicationContext();
SharedPreferences mSharedPreferences = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
sPreviousDriveID = mSharedPreferences.getString(PREF_DRIVE_ID,"");
if (sPreviousDriveID.length() > 0){
Log.v(TAG, "Using saved drive ID: " + sPreviousDriveID);
Drive.DriveApi.fetchDriveId(getGoogleApiClient(),sPreviousDriveID)
.setResultCallback(driveFetchIDCallback);
} else {
// create new contents resource
Log.v(TAG, "Attempting to create new drive file");
Drive.DriveApi.newDriveContents(getGoogleApiClient())
.setResultCallback(driveContentsCallback);
}
}
示例4: getContentsFromDrive
import com.google.android.gms.drive.DriveApi; //导入依赖的package包/类
public String getContentsFromDrive(DriveId id) throws IOException {
DriveFile theFile = Drive.DriveApi.getFile(mGoogleApiClient, id);
DriveApi.DriveContentsResult result =
theFile.open(mGoogleApiClient, DriveFile.MODE_READ_ONLY, null).await();
DriveContents driveContents = result.getDriveContents();
try {
if (driveContents != null) {
return IOUtils.readAsString(driveContents.getInputStream());
}
} finally {
if (driveContents != null) {
driveContents.discard(mGoogleApiClient);
}
}
return null;
}
示例5: createEmptyDriveFile
import com.google.android.gms.drive.DriveApi; //导入依赖的package包/类
/**
* Create an empty file with the given {@code fileName} and {@code mimeType}.
*
* @return {@link DriveId} of the specific file.
*/
private DriveId createEmptyDriveFile(String fileName, String mimeType) {
DriveApi.DriveContentsResult result =
Drive.DriveApi.newDriveContents(mGoogleApiClient).await();
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle(fileName)
.setMimeType(mimeType)
.setStarred(true)
.build();
// Create a new file with the given changeSet in the AppData folder.
DriveFolder.DriveFileResult driveFileResult = Drive.DriveApi.getAppFolder(mGoogleApiClient)
.createFile(mGoogleApiClient, changeSet, result.getDriveContents())
.await();
return driveFileResult.getDriveFile().getDriveId();
}
示例6: commitToNewFile
import com.google.android.gms.drive.DriveApi; //导入依赖的package包/类
/**
* Creates a new file and adds the {@link DriveContents} passed to this method.
* @return Returns a boolean stating the success of this operation
*/
private boolean commitToNewFile(DriveContents driveContents) {
DebugLog.logMethod();
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle("CouponsTrackerData.txt")
.setMimeType("text/plain")
.build();
DriveFolder.DriveFileResult driveFileResult = Drive.DriveApi
.getAppFolder(getGoogleApiClient())
.createFile(getGoogleApiClient(), changeSet, driveContents)
.await();
DebugLog.logMessage("DriveFileResult: statusCode - " + driveFileResult.getStatus().getStatusCode()
+ ", statusMessage: " + driveFileResult.getStatus().getStatusMessage());
return driveFileResult.getStatus().isSuccess();
}
示例7: getDriveFile
import com.google.android.gms.drive.DriveApi; //导入依赖的package包/类
/**
* Returns the app specific {@link DriveFile} if it has already been created. Else
* returns null.
*/
protected final DriveFile getDriveFile() {
DebugLog.logMethod();
DriveApi.MetadataBufferResult metadataBufferResult = Drive.DriveApi.getAppFolder(getGoogleApiClient())
.listChildren(getGoogleApiClient())
.await();
DebugLog.logMessage("Status code: " + metadataBufferResult.getStatus().getStatusCode()
+ "\nStatus message: " + metadataBufferResult.getStatus().getStatusMessage());
if (!metadataBufferResult.getStatus().isSuccess()) {
metadataBufferResult.release();
DebugLog.logMessage("MetadataBufferResult failure");
return null;
}
MetadataBuffer metadataBuffer = metadataBufferResult.getMetadataBuffer();
DebugLog.logMessage("MetadataBuffer count: " + metadataBuffer.getCount());
if (metadataBuffer.getCount() == 0) {
metadataBuffer.release();
return null;
}
DriveId driveId = metadataBuffer.get(0).getDriveId();
metadataBuffer.release();
metadataBufferResult.release();
return driveId.asDriveFile();
}
示例8: doInBackgroundConnected
import com.google.android.gms.drive.DriveApi; //导入依赖的package包/类
@Override
protected Boolean doInBackgroundConnected(EditContentParams... args) {
DriveFile file = args[0].getFileToWrite();
String dataToWrite = args[0].getDataToWrite();
try {
DriveApi.DriveContentsResult driveContentsResult = file.open(
getGoogleApiClient(), DriveFile.MODE_WRITE_ONLY, null).await();
if (!driveContentsResult.getStatus().isSuccess()) {
return false;
}
DriveContents driveContents = driveContentsResult.getDriveContents();
OutputStream outputStream = driveContents.getOutputStream();
outputStream.write(dataToWrite.getBytes());
com.google.android.gms.common.api.Status status =
driveContents.commit(getGoogleApiClient(), null).await();
return status.getStatus().isSuccess();
} catch (IOException e) {
AppUtils.showMessage(context,"Failed to write data to file.");
}
return false;
}
示例9: queryFile
import com.google.android.gms.drive.DriveApi; //导入依赖的package包/类
private void queryFile() {
Query query = new Query.Builder()
.addFilter(Filters.contains(SearchableField.TITLE, "Note.json"))
.build();
Drive.DriveApi.query(
mGoogleApiClient, query).setResultCallback(result -> {
if (!result.getStatus().isSuccess()) {
if (BuildConfig.DEBUG)
Log.d(TAG, "Problem while retrieving results");
return;
}
Metadata metadata = result.getMetadataBuffer().get(0);
if (BuildConfig.DEBUG)
Log.d(TAG, "metadata.getDriveId():" + metadata.getDriveId());
readFileFromDrive(metadata.getDriveId());
});
}
示例10: doInBackgroundConnected
import com.google.android.gms.drive.DriveApi; //导入依赖的package包/类
@Override
protected String doInBackgroundConnected(DriveId... params) {
String contents = null;
DriveFile file = params[0].asDriveFile();
DriveApi.DriveContentsResult driveContentsResult =
file.open(getGoogleApiClient(), DriveFile.MODE_READ_ONLY, null).await();
if (!driveContentsResult.getStatus().isSuccess()) {
return null;
}
DriveContents driveContents = driveContentsResult.getDriveContents();
BufferedReader reader = new BufferedReader(
new InputStreamReader(driveContents.getInputStream()));
StringBuilder builder = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
builder.append(line);
}
contents = builder.toString();
} catch (IOException e) {
Log.e(TAG, "IOException while reading from the stream", e);
}
driveContents.discard(getGoogleApiClient());
return contents;
}
示例11: doInBackgroundConnected
import com.google.android.gms.drive.DriveApi; //导入依赖的package包/类
@Override
protected Boolean doInBackgroundConnected(DriveFile... args) {
DriveFile file = args[0];
try {
DriveApi.DriveContentsResult driveContentsResult = file.open(
getGoogleApiClient(), DriveFile.MODE_WRITE_ONLY, null).await();
if (!driveContentsResult.getStatus().isSuccess()) {
return false;
}
DriveContents driveContents = driveContentsResult.getDriveContents();
OutputStream outputStream = driveContents.getOutputStream();
outputStream.write(Helpers.getSMSJson(context).getBytes());
com.google.android.gms.common.api.Status status =
driveContents.commit(getGoogleApiClient(), null).await();
return status.getStatus().isSuccess();
} catch (IOException e) {
Log.e(TAG, "IOException while appending to the output stream", e);
}
return false;
}
示例12: doAfterConnectedStuff
import com.google.android.gms.drive.DriveApi; //导入依赖的package包/类
private void doAfterConnectedStuff() {
if (progressDialog == null)
progressDialog = new ProgressDialog(this);
if (type == Constants.TYPE_BACKUP)
progressDialog.setTitle(getString(R.string.google_drive_backup));
else
progressDialog.setTitle(getString(R.string.google_drive_restore));
progressDialog.setMessage(getString(R.string.please_wait));
progressDialog.setIndeterminate(true);
progressDialog.show();
Drive.DriveApi.newDriveContents(getGoogleApiClient()).setResultCallback(driveContentsCallback);
}
示例13: onConnected
import com.google.android.gms.drive.DriveApi; //导入依赖的package包/类
@Override
public void onConnected(Bundle connectionHint) {
Log.i(TAG, "API client connected.");
//when the client is connected i have two possibility: backup (bckORrst -> true) or restore (bckORrst -> false)
if (bckORrst)
saveFileToDrive();
else {
IntentSender intentSender = Drive.DriveApi
.newOpenFileActivityBuilder()
.setMimeType(new String[]{"application/db"})
.build(mGoogleApiClient);
try {
startIntentSenderForResult(intentSender, REQUEST_CODE_OPENER, null, 0, 0, 0);
Log.i(TAG, "Open File Intent send");
} catch (IntentSender.SendIntentException e) {
Log.w(TAG, "Unable to send Open File Intent", e);
}
}
}
示例14: onResult
import com.google.android.gms.drive.DriveApi; //导入依赖的package包/类
/**
* Handles the result of the query. If the results are nonzero, passes up the {@link Metadata}
* out of the result one at a time through {@link QuerierResultCallback#onQuerierResult(Metadata)}
* with the {@link QuerierResultCallback} registered in ths {@link Querier}. If the results are
* null, calls {@link QuerierResultCallback#onNoQuerierResult()}
*
* @param result the result of the query
*/
@Override
public void onResult(DriveApi.MetadataBufferResult result) {
if (!result.getStatus().isSuccess()) {
Log.e("Querier", "Problem while retrieving files");
return;
}
// Get the metadata buffer
MetadataBuffer buffer = result.getMetadataBuffer();
// Check for file dne
if (buffer.getCount() == 0) {
// Call null result callback
callback.onNoQuerierResult();
}
// Get the metadata
for (Metadata m : buffer) {
if (m.isInAppFolder() && !m.isTrashed()) {
callback.onQuerierResult(m);
break;
}
}
}
示例15: onFileResultsReady
import com.google.android.gms.drive.DriveApi; //导入依赖的package包/类
/**
* Handles the DriveContentsResult returned from the request to get the DriveFile of the
* Database.
*
* @param result the DriveContentsResult representing the Drive copy of the Database
*/
@Override
public void onFileResultsReady(DriveApi.DriveContentsResult result) {
if (!mDriveClient.isConnected()) {
mDriveClient.connect();
}
this.result = result;
int which = deQueue();
switch (which) {
case PUT:
writeLocalDbToCloudStream(result.getDriveContents().getOutputStream());
result.getDriveContents().commit(mDriveClient, null);
break;
case GET:
writeCloudStreamToLocalDb(result.getDriveContents().getInputStream());
break;
}
if (ongoingRequest) {
mDriveLayer.getFile(localDb.getName());
}
}