本文整理匯總了Java中com.google.android.gms.drive.DriveFile類的典型用法代碼示例。如果您正苦於以下問題:Java DriveFile類的具體用法?Java DriveFile怎麽用?Java DriveFile使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
DriveFile類屬於com.google.android.gms.drive包,在下文中一共展示了DriveFile類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: saveDriveFile
import com.google.android.gms.drive.DriveFile; //導入依賴的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: getContentsFromDrive
import com.google.android.gms.drive.DriveFile; //導入依賴的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: getDriveFile
import com.google.android.gms.drive.DriveFile; //導入依賴的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();
}
示例4: onResult
import com.google.android.gms.drive.DriveFile; //導入依賴的package包/類
@Override
public void onResult(@NonNull final DriveApi.DriveContentsResult driveContentsResult) {
new Thread(new Runnable() {
@Override
public void run() {
DriveFolder appFolder = Drive.DriveApi.getAppFolder(mGoogleApiClient);
MetadataChangeSet set = new MetadataChangeSet.Builder().setTitle(getString(R.string.drive_file_name)).setMimeType(getString(R.string.drive_file_mimetype)).build();
appFolder.createFile(mGoogleApiClient, set, driveContentsResult.getDriveContents()).setResultCallback(new ResultCallback<DriveFolder.DriveFileResult>() {
@Override
public void onResult(@NonNull DriveFolder.DriveFileResult driveFileResult) {
driveFileResult.getDriveFile().open(mGoogleApiClient, DriveFile.MODE_WRITE_ONLY, null).setResultCallback(writeDriveCallback);
loadingView.setVisibility(View.GONE);
}
});
}
}).start();
}
示例5: doInBackgroundConnected
import com.google.android.gms.drive.DriveFile; //導入依賴的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.DriveFile; //導入依賴的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.DriveFile; //導入依賴的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: getDriveFile
import com.google.android.gms.drive.DriveFile; //導入依賴的package包/類
/**
* Get a file from the Drive folder.
*
* @param hash The file hash
* @return The DriveFile or null if it doesn't exist.
*/
@Nullable
private DriveFile getDriveFile(@NonNull String hash) {
if(mResourceClient == null || mDriveFolder == null) {
return null;
}
try {
final Query query = new Query.Builder().addFilter(Filters.eq(sHashKey, hash)).build();
final MetadataBuffer buffer =
Tasks.await(mResourceClient.queryChildren(mDriveFolder, query));
if(buffer != null && buffer.getCount() > 0) {
final DriveFile driveFile = buffer.get(0).getDriveId().asDriveFile();
buffer.release();
return driveFile;
}
} catch(ExecutionException | InterruptedException ignored) {
}
return null;
}
示例9: onActivityResult
import com.google.android.gms.drive.DriveFile; //導入依賴的package包/類
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
switch (requestCode) {
case REQUEST_CODE_CREATOR:
// Called after a file is saved to Drive.
if (resultCode == RESULT_OK) {
Log.i(TAG, "Backup successfully saved.");
Toast.makeText(this, "Backup successufly loaded!", Toast.LENGTH_SHORT).show();
}
break;
case REQUEST_CODE_OPENER:
if (resultCode == RESULT_OK) {
DriveId driveId = data.getParcelableExtra(
OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);
//Toast.makeText(this, driveId.toString(), Toast.LENGTH_SHORT).show();
DriveFile file = driveId.asDriveFile();
importFromDrive(file);
}
}
}
示例10: DownloadFile
import com.google.android.gms.drive.DriveFile; //導入依賴的package包/類
public void DownloadFile(DriveId driveId, final OnFileDownloaded callback) {
activeOperations = true;
PendingResult<DriveApi.DriveContentsResult> contentsResult = driveId.asDriveFile().open(getApiClient(), DriveFile.MODE_READ_ONLY, null);
contentsResult.setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() {
@Override
public void onResult(DriveApi.DriveContentsResult driveContentsResult) {
Log.d("DocumentList", "In file open");
if (driveContentsResult.getDriveContents() == null) {
//There is no file
callback.onFileDownloaded(null);
activeOperations = false;
return;
}
callback.onFileDownloaded(Utils.readFromInputStream(driveContentsResult.getDriveContents().getInputStream()));
activeOperations = false;
}
});
}
示例11: UploadFileExisting
import com.google.android.gms.drive.DriveFile; //導入依賴的package包/類
private void UploadFileExisting(final DriveId driveId, final String contents, final String fileName, final OnFileUploaded callback) {
driveId.asDriveFile().open(getApiClient(), DriveFile.MODE_WRITE_ONLY, null).setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() {
@Override
public void onResult(DriveApi.DriveContentsResult driveContentsResult) {
Utils.writeToOutputStream(driveContentsResult.getDriveContents().getOutputStream(), contents);
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle(fileName)
.setPinned(true)
.build();
driveContentsResult.getDriveContents().commit(getApiClient(), changeSet).setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status result) {
if (result.getStatus().isSuccess())
callback.onFileUploaded(driveId);
else
callback.onFileUploaded(null);
activeOperations = false;
}
});
}
});
}
示例12: deleteFolder
import com.google.android.gms.drive.DriveFile; //導入依賴的package包/類
public Observable<Void> deleteFolder(final String folderName) {
return Observable.defer(new Func0<Observable<Void>>() {
@Override
public Observable<Void> call() {
GoogleApiClient client = googleApiClientManager.getGoogleApiClient();
// find folder drive id
DriveFolder appFolder = getAppRootFolder();
MetadataBuffer queryResult = appFolder.queryChildren(
client,
new Query.Builder().addFilter(Filters.eq(SearchableField.TITLE, folderName)).build())
.await().getMetadataBuffer();
if (queryResult.getCount() == 0) throw new IllegalArgumentException("no folder with name " + folderName);
DriveId folderId = queryResult.get(0).getDriveId();
// delete folder
DriveFile folder = Drive.DriveApi.getFile(client, folderId);
folder.delete(client).await();
return Observable.just(null);
}
});
}
示例13: writeFile
import com.google.android.gms.drive.DriveFile; //導入依賴的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);
}
});
}
示例14: createFile
import com.google.android.gms.drive.DriveFile; //導入依賴的package包/類
private void createFile() {
setUiInteractionsEnabled(false);
int fileCount = mFileFolderAdapter.getCount();
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle("sample file " + (fileCount + 1))
.setMimeType("text/plain")
.build();
DriveFolder driveFolder = mNavigationPath.peek().asDriveFolder();
Task<DriveFile> createFileTask =
mDriveResourceClient.createFile(driveFolder, changeSet, null);
Task<Void> updateTask = updateUiAfterTask(createFileTask);
handleTaskError(updateTask, R.string.unexpected_error);
}
示例15: readFileFromDrive
import com.google.android.gms.drive.DriveFile; //導入依賴的package包/類
@DebugLog
private void readFileFromDrive(final MetadataBuffer meta_data, final DriveId drive_id) {
Drive.DriveApi.getFile(_parent.getClient(), drive_id)
.open(_parent.getClient(), DriveFile.MODE_READ_ONLY, null)
.setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() {
@Override
public void onResult(DriveApi.DriveContentsResult driveContentsResult) {
try {
InputStream input = driveContentsResult.getDriveContents()
.getInputStream();
String total = consumeStream(input);
AmiiboFile amiibo_file = AmiiboFile.fromString(total.toString());
_parent.onFileRead(amiibo_file);
if (!meta_data.isClosed()) meta_data.release();
} catch (IOException e) {
e.printStackTrace();
_parent.onFileRead(null);
}
}
});
}