我在将Google Drive API集成到Android应用中时遇到了一些问题。基本上,我试图创建一个文件夹来存储我的错误日志。问题是偶尔会运行线程,并且在文件需要创建时不会创建文件夹,因此应用程序崩溃。我试图考虑如何避免这种情况,并尝试了一些方法到目前为止,包括在文件夹回调中进行文件夹分配,但都无济于事。我有时很幸运(特别是在调试时,不幸的是)并且文件保存正常,但我显然需要解决并发问题。如果上下文需要更多代码,请告诉我。
protected void onPause() {
// TODO Find the right place for this
if (!mDriveLogList.isEmpty()) {
// Perform I/O off the UI thread.
// TODO Fix to use RxJava
new Thread() {
@Override
public void run() {
Timber.i("Starting File Creation");
// write content to DriveContents
if (mDriveContents == null) {
Timber.e("Drive Contents Were Null");
} else {
OutputStream outputStream = mDriveContents.getOutputStream();
Writer writer = new OutputStreamWriter(outputStream);
//TODO Clean/Refactor
try {
writer.write("Status Log For: " + DateHelper.getDate() + "\n");
for (String logEntry : mDriveLogList) {
writer.write(logEntry + "\n");
}
writer.close();
} catch (IOException e) {
Timber.e(e, "Error During Drive Contents Callback");
}
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle(SURVEIL_DROID + " " + DateHelper.getDate())
.setMimeType("text/plain")
.setStarred(true).build();
MetadataChangeSet folderSet = new MetadataChangeSet.Builder()
.setTitle("SurveilCustomFolder")
.build();
Drive.DriveApi.getRootFolder(mGoogleApiClient)
.createFolder(mGoogleApiClient, folderSet)
.setResultCallback(folderCallback);
mDriveFolder = mFolderId.asDriveFolder();
mDriveFolder.createFile(mGoogleApiClient, changeSet, mDriveContents)
.setResultCallback(fileCallback);
mDriveLogList.clear();
}
}
}.start();
答案 0 :(得分:1)
您可以使用倒计时(请参阅https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CountDownLatch.html)来帮助进行同步。像
这样的东西protected void onPause() {
// TODO Find the right place for this
if (!mDriveLogList.isEmpty()) {
// Perform I/O off the UI thread.
// TODO Fix to use RxJava
new Thread() {
@Override
public void run() {
final protected CountDownLatch connectionLatch = new CountDownLatch(1);
protected boolean folderCreated = false;
Timber.i("Starting File Creation");
// write content to DriveContents
if (mDriveContents == null) {
Timber.e("Drive Contents Were Null");
} else {
OutputStream outputStream = mDriveContents.getOutputStream();
Writer writer = new OutputStreamWriter(outputStream);
//TODO Clean/Refactor
try {
writer.write("Status Log For: " + DateHelper.getDate() + "\n");
for (String logEntry : mDriveLogList) {
writer.write(logEntry + "\n");
}
writer.close();
} catch (IOException e) {
Timber.e(e, "Error During Drive Contents Callback");
}
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle(SURVEIL_DROID + " " + DateHelper.getDate())
.setMimeType("text/plain")
.setStarred(true).build();
MetadataChangeSet folderSet = new MetadataChangeSet.Builder()
.setTitle("SurveilCustomFolder")
.build();
Drive.DriveApi.getRootFolder(mGoogleApiClient)
.createFolder(mGoogleApiClient, folderSet)
.setResultCallback(new
ResultCallback<DriveFolderResult>() {
@Override
public void onResult(DriveFolderResult result) {
folderCreated = result.getStatus().isSuccess();
connectionLatch.countDown(); // Release latch
}
});
try {
connectionLatch.await() ;
} catch (InterruptedException e) {
e.printStackTrace();
return;
}
if (folderCreated) {
mDriveFolder = mFolderId.asDriveFolder();
mDriveFolder.createFile(mGoogleApiClient, changeSet, mDriveContents)
.setResultCallback(fileCallback);
mDriveLogList.clear();
}
}
}
}.start();
}
}
应该有用。
答案 1 :(得分:0)
createFolder()返回一个PendingResult,您可以使用PendingResult的一个await()方法来阻止,直到创建该文件夹。见这里:(https://developers.google.com/android/reference/com/google/android/gms/common/api/PendingResult.html#await())