本文整理汇总了Java中com.google.android.gms.drive.DriveContents.getOutputStream方法的典型用法代码示例。如果您正苦于以下问题:Java DriveContents.getOutputStream方法的具体用法?Java DriveContents.getOutputStream怎么用?Java DriveContents.getOutputStream使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.android.gms.drive.DriveContents
的用法示例。
在下文中一共展示了DriveContents.getOutputStream方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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);
}
示例2: 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;
}
示例3: 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;
}
示例4: getOutputStream
import com.google.android.gms.drive.DriveContents; //导入方法依赖的package包/类
/**
* Gets an output stream to the Drive file contents.
*
* @param driveFileContent
* @return
*/
private OutputStream getOutputStream(DriveContents driveFileContent) {
OutputStream res = null;
switch (driveFileContent.getMode()) {
case DriveFile.MODE_READ_ONLY:
break;
case DriveFile.MODE_READ_WRITE:
//Use this mode to append data to a file
res = new FileOutputStream(driveFileContent.getParcelFileDescriptor().getFileDescriptor());
break;
case DriveFile.MODE_WRITE_ONLY:
//Use this mode to truncate the file before writing in it
OutputStream outputStream = driveFileContent.getOutputStream();
res = new BufferedOutputStream(outputStream);
break;
}
return res;
}
示例5: writeSums
import com.google.android.gms.drive.DriveContents; //导入方法依赖的package包/类
/**
* Writes the current sum to the specified {@code driveContents}.
*/
private void writeSums(final DriveContents driveContents) {
OutputStream outputStream = driveContents.getOutputStream();
try (Writer writer = new OutputStreamWriter(outputStream)) {
for (int i = 0; i < mSumAdapter.getCount(); i++) {
writer.write(mSumAdapter.getItem(i) + "\n");
}
} catch (IOException e) {
Log.e(TAG, "Error occurred while writing to driveContents.", e);
}
}
示例6: uploadFile
import com.google.android.gms.drive.DriveContents; //导入方法依赖的package包/类
@Override
public int uploadFile(File tempFile) {
DriveFile driveFile;
MetadataResult metadataResult;
Metadata metadata;
DriveContents driveContents = null;
DriveContentsResult driveContentsResult;
OutputStream outputStream;
ExecutionOptions executionOptions;
int complentionStatus;
Log.i(TAG, "start upload of DB file temp:" + tempFile.getName());
try {
driveFile = mDriveId.asDriveFile();
// Get metadata
metadataResult = driveFile.getMetadata(mGoogleApiClient).await();
checkStatus(metadataResult);
metadata = metadataResult.getMetadata();
Log.i(TAG, "try to upload of DB file:" + metadata.getOriginalFilename());
if (!metadata.isPinned()) {
pinFile(driveFile);
}
// Writing file
driveContentsResult = mDriveContent.reopenForWrite(mGoogleApiClient).await();
checkStatus(driveContentsResult);
driveContents = driveContentsResult.getDriveContents();
outputStream = driveContents.getOutputStream();
// Copio il file
FileUtils.copyFile(tempFile, outputStream);
Log.i(TAG, "try to commit file copy done");
// Commit del file
executionOptions = new ExecutionOptions.Builder()
.setNotifyOnCompletion(true)
.setConflictStrategy(ExecutionOptions.CONFLICT_STRATEGY_KEEP_REMOTE)
.build();
driveContents.commit(mGoogleApiClient, null, executionOptions);
Log.i(TAG, "file committed - wait for complention");
// Wait for complention
complentionStatus = mGDriveCompletionRecevier.waitCompletion();
Log.i(TAG, "received complention status:" + complentionStatus);
if (complentionStatus == CompletionEvent.STATUS_CONFLICT) {
return UPLOAD_CONFLICT;
} else if (complentionStatus == CompletionEvent.STATUS_FAILURE) {
throw new SyncException(SyncStatus.Code.ERROR_UPLOAD_CLOUD, "Error writing file to GDrive (FAILURE of commit)");
}
return UPLOAD_OK;
} catch (IOException e) {
if (driveContents != null) {
driveContents.discard(mGoogleApiClient);
}
throw new SyncException(SyncStatus.Code.ERROR_UPLOAD_CLOUD, "Error writing file to GDrive message:" + e.getMessage());
}
}
示例7: uploadFile
import com.google.android.gms.drive.DriveContents; //导入方法依赖的package包/类
/**
* Upload a file to Drive if it does not already exist.
*
* @param uri The Uri to the image file
* @param hash The file hash
* @return The DriveFile
*/
@Nullable
private DriveFile uploadFile(@NonNull Uri uri, @NonNull String hash) {
DriveFile driveFile = getDriveFile(hash);
if(driveFile != null) {
return driveFile;
}
if(mResourceClient == null || !mShouldSync || !mMediaMounted) {
return null;
}
final ContentResolver cr = mContext.getContentResolver();
final String name = PhotoUtils.getName(cr, uri);
if(name == null) {
return null;
}
DriveContents driveContents = null;
try {
driveContents = Tasks.await(mResourceClient.createContents());
final InputStream inputStream = cr.openInputStream(uri);
if(inputStream == null) {
return null;
}
final OutputStream outputStream = driveContents.getOutputStream();
try {
final byte[] buffer = new byte[8192];
int read;
while((read = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, read);
}
} finally {
inputStream.close();
outputStream.close();
}
createNewFile(name, hash, driveContents);
return null;
} catch(ExecutionException | InterruptedException | IOException e) {
Log.w(TAG, "Upload failed", e);
}
if(driveContents != null) {
mResourceClient.discardContents(driveContents);
}
return null;
}
示例8: uploadNewLocalItems
import com.google.android.gms.drive.DriveContents; //导入方法依赖的package包/类
@WorkerThread
private boolean uploadNewLocalItems(GoogleApiClient googleApiClient, ArrayList<String> newLocalItems) {
Log.d();
int itemsCount = newLocalItems.size();
int itemIndex = 0;
for (String uuid : newLocalItems) {
if (mListener != null) mListener.onUploadNewLocalItemsProgress(itemIndex, itemsCount);
Log.d("Creating " + uuid);
DriveApi.DriveContentsResult driveContentsResult = Drive.DriveApi.newDriveContents(googleApiClient).await(AWAIT_DELAY_LONG, AWAIT_UNIT_LONG);
Status status = driveContentsResult.getStatus();
Log.d("driveContentsResult.status=" + status);
if (!status.isSuccess()) {
Log.w("Could not create new Drive contents");
return false;
}
RideSelection rideSelection = new RideSelection();
rideSelection.uuid(uuid);
RideCursor rideCursor = rideSelection.query(mContext);
rideCursor.moveToFirst();
Uri rideUri = ContentUris.withAppendedId(RideColumns.CONTENT_URI, rideCursor.getId());
rideCursor.close();
Log.d("rideUri=" + rideUri);
DriveContents driveContents = driveContentsResult.getDriveContents();
OutputStream outputStream = driveContents.getOutputStream();
BikeyExporter exporter = new BikeyExporter(rideUri);
exporter.setOutputStream(outputStream);
try {
exporter.export();
outputStream.flush();
IoUtil.closeSilently(outputStream);
} catch (IOException e) {
Log.w("Could not export to Drive contents", e);
return false;
}
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle(uuid + EXTENSION)
.setMimeType(MIME_TYPE).build();
DriveFolder.DriveFileResult driveFileResult =
Drive.DriveApi.getAppFolder(googleApiClient).createFile(googleApiClient, changeSet, driveContents).await(AWAIT_DELAY_LONG,
AWAIT_UNIT_LONG);
status = driveFileResult.getStatus();
Log.d("driveFileResult.status=" + status);
if (!status.isSuccess()) {
Log.w("Could not create new Drive file");
return false;
}
itemIndex++;
if (mListener != null) mListener.onUploadNewLocalItemsProgress(itemIndex, itemsCount);
if (mAbortRequested) {
if (mListener != null) mListener.onSyncFinish(false);
return false;
}
}
return true;
}