本文整理汇总了Java中com.google.android.gms.drive.DriveContents类的典型用法代码示例。如果您正苦于以下问题:Java DriveContents类的具体用法?Java DriveContents怎么用?Java DriveContents使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DriveContents类属于com.google.android.gms.drive包,在下文中一共展示了DriveContents类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onResult
import com.google.android.gms.drive.DriveContents; //导入依赖的package包/类
@Override
public void onResult(DriveContentsResult result) {
if (!result.getStatus().isSuccess()) {
Log.i(TAG, "Error creating new file contents");
return;
}
final DriveContents driveContents = result.getDriveContents();
new Thread() {
@Override
public void run() {
OutputStream outputStream = driveContents.getOutputStream();
addTextfileToOutputStream(outputStream);
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle("testFile")
.setMimeType("text/plain")
.setDescription("This is a text file uploaded from device")
.setStarred(true).build();
Drive.DriveApi.getRootFolder(googleApiClient)
.createFile(googleApiClient, changeSet, driveContents)
.setResultCallback(fileCallback);
}
}.start();
}
示例2: getContentsFromDrive
import com.google.android.gms.drive.DriveContents; //导入依赖的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;
}
示例3: writeToDriveFile
import com.google.android.gms.drive.DriveContents; //导入依赖的package包/类
/**
* Write the couponsJson to the driveContents, creating or updating the file as required.
* Returns a boolean indicating success of this task.
*/
private boolean writeToDriveFile(DriveContents driveContents, String couponsJson, boolean isNewFile) {
DebugLog.logMethod();
OutputStream outputStream = driveContents.getOutputStream();
/*
Ref: http://stackoverflow.com/a/4069104/3946664
Avoiding usage of outputStream.write() since it requires a byte array to be
passed. In case of String, the getBytes() should be avoided since it uses
default encoding of the JVM which cannot be reliably predicted in a portable
manner. Hence using Writer's write method.
*/
Writer writer = new OutputStreamWriter(outputStream);
try {
writer.write(couponsJson);
writer.close();
} catch (IOException e) {
e.printStackTrace();
DebugLog.logMessage(e.getMessage());
return false;
}
return isNewFile ? commitToNewFile(driveContents) : commitToExistingFile(driveContents);
}
示例4: commitToNewFile
import com.google.android.gms.drive.DriveContents; //导入依赖的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();
}
示例5: doInBackgroundConnected
import com.google.android.gms.drive.DriveContents; //导入依赖的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;
}
示例6: doInBackgroundConnected
import com.google.android.gms.drive.DriveContents; //导入依赖的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;
}
示例7: doInBackgroundConnected
import com.google.android.gms.drive.DriveContents; //导入依赖的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;
}
示例8: createNewFile
import com.google.android.gms.drive.DriveContents; //导入依赖的package包/类
/**
* Create a new file on Drive.
*
* @param title The name of the file
* @param hash The file hash
* @param contents The file content
*/
private void createNewFile(@NonNull String title, @NonNull String hash,
@NonNull DriveContents contents) {
if(mResourceClient == null || mDriveFolder == null) {
return;
}
final MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle(title)
.setCustomProperty(sHashKey, hash).build();
final ExecutionOptions options = new ExecutionOptions.Builder()
.setNotifyOnCompletion(true).build();
try {
Tasks.await(mResourceClient.createFile(mDriveFolder, changeSet, contents, options));
} catch(ExecutionException | InterruptedException e) {
Log.w(TAG, "Failed to create remote file", e);
}
}
示例9: writeFile
import com.google.android.gms.drive.DriveContents; //导入依赖的package包/类
public Observable<Void> writeFile(final DriveId driveId, final InputStream inputStream) {
return Observable.defer(new Func0<Observable<Void>>() {
@Override
public Observable<Void> call() {
// OutputStream outputStream = Drive.DriveApi.getFile(googleApiClientManager.getGoogleApiClient(), driveId)
DriveContents driveContents = Drive.DriveApi.getFile(googleApiClientManager.getGoogleApiClient(), driveId)
.open(googleApiClientManager.getGoogleApiClient(), DriveFile.MODE_WRITE_ONLY, null)
.await()
.getDriveContents();
// copy file to drive contents
try {
copyStream(inputStream, driveContents.getOutputStream());
} catch (IOException e) {
Timber.e(e, "failed to read image");
return Observable.error(e);
}
// commit changes
driveContents.commit(googleApiClientManager.getGoogleApiClient(), new MetadataChangeSet.Builder().build()).await();
return Observable.just(null);
}
});
}
示例10: doInBackgroundConnected
import com.google.android.gms.drive.DriveContents; //导入依赖的package包/类
@Override
protected String doInBackgroundConnected(DriveId... params) {
String contents = null;
DriveFile file = Drive.DriveApi.getFile(getGoogleApiClient(), params[0]);
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) {
}
driveContents.discard(getGoogleApiClient());
return contents;
}
示例11: updateSums
import com.google.android.gms.drive.DriveContents; //导入依赖的package包/类
/**
* Updates the App Data file associated with {@code driveContents} with the current sum.
*/
private void updateSums(DriveContents driveContents) {
writeSums(driveContents);
mDriveResourceClient.commitContents(driveContents, /* metadataChangeSet= */ null)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "App data successfully updated");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e(TAG, "Unable to write contents.", e);
}
});
}
示例12: loadContents
import com.google.android.gms.drive.DriveContents; //导入依赖的package包/类
private Task<Void> loadContents(DriveFile file) {
mGroceryListFile = file;
Task<DriveContents> loadTask =
getDriveResourceClient().openFile(file, DriveFile.MODE_READ_ONLY);
return loadTask.continueWith(new Continuation<DriveContents, Void>() {
@Override
public Void then(@NonNull Task<DriveContents> task) throws Exception {
Log.d(TAG, "Reading file contents");
mDriveContents = task.getResult();
InputStream inputStream = mDriveContents.getInputStream();
String groceryListStr = ConflictUtil.getStringFromInputStream(inputStream);
mEditText.setText(groceryListStr);
return null;
}
});
}
示例13: doInBackground
import com.google.android.gms.drive.DriveContents; //导入依赖的package包/类
/**
* Opens contents for the given file, executes the editing tasks, saves the
* metadata and content changes.
*/
@Override
protected Boolean doInBackground(DriveId... params) {
DriveFile file = params[0].asDriveFile();
try {
DriveContents contents =
Tasks.await(driveResourceClient.openFile(file, DriveFile.MODE_WRITE_ONLY));
Changes changes = edit(contents);
MetadataChangeSet changeSet = changes.getMetadataChangeSet();
DriveContents updatedContents = changes.getDriveContents();
if (changeSet != null) {
Tasks.await(driveResourceClient.updateMetadata(file, changeSet));
}
if (updatedContents != null) {
Tasks.await(driveResourceClient.commitContents(updatedContents, changeSet));
}
} catch (ExecutionException | InterruptedException e) {
Log.e(TAG, "Error editing DriveFile.", e);
return false;
}
return true;
}
示例14: sync_getFileContents
import com.google.android.gms.drive.DriveContents; //导入依赖的package包/类
/**
* Gets the file contents, see {@link DriveContents}
*
* @param driveFile The Drive file.
* @param openMode The open mode. See {@link DriveFile}
* @param downloadProgressListener If set, opening progress
* information can be show.
* @returns The Drive file contents, see {@link DriveContents}
*/
public DriveContents sync_getFileContents(DriveFile driveFile, int openMode,
DownloadProgressListener downloadProgressListener) throws TBDriveException {
DriveContentsResult result = null;
if(downloadProgressListener!=null) {
result = driveFile.open(getGoogleApiClient(), openMode, downloadProgressListener).await();
}else{
result = driveFile.open(getGoogleApiClient(), openMode, null).await();
}
if (!result.getStatus().isSuccess()) {
//Display an error saying file can't be opened
Log.e(TAG, "Problem opening the file [" + result.getStatus().getStatusMessage() + "].");
throw new TBDriveException("Problem opening the file [" + result.getStatus().getStatusMessage() + "].");
}
//DriveContents object contains pointers to the byte stream
return result.getDriveContents();
}
示例15: drive_replaceTextOfFile
import com.google.android.gms.drive.DriveContents; //导入依赖的package包/类
/**
* Replaces the text content of the file with the specified new string content data.
*
* <b>NOTE</b>:<br><br>
* To use this method, Drive file must be opened in MODE_WRITE_ONLY.
*
* @param driveContent The Drive file contents, see {@link DriveContents}
* @param data
* @return The modified contents.
* @throws IOException In case of any error or bad open mode.
*/
public synchronized DriveContents drive_replaceTextOfFile(DriveContents driveContent, String data) throws IOException {
Writer writer = null;
if(driveContent.getMode()==DriveFile.MODE_WRITE_ONLY) {
try {
ParcelFileDescriptor parcelFileDescriptor = driveContent.getParcelFileDescriptor();
//Append new data content
FileOutputStream fileOutputStream = new FileOutputStream(parcelFileDescriptor.getFileDescriptor());
writer = new OutputStreamWriter(fileOutputStream);
writer.write(data);
} finally {
try {
writer.close();
}catch(Exception e){}
}
}else{
throw new IOException("Replacing whole content of a file needs MODE_WRITE_ONLY open mode.");
}
//Returns the modified contents
return driveContent;
}