我正在尝试从“我的Google云端硬盘”中获取文件的内容,但每次尝试获取数据时都会向我提供异常提供的无效模式
我已经使用了所有可用的模式,但没有运气 下面是我从我的文件中获取内容的代码
@Override
public void onConnected(@Nullable Bundle bundle) {
Drive.DriveApi.newDriveContents(mGoogleApiClient).setResultCallback(driveContentsCallback);
}
final ResultCallback<DriveApi.DriveContentsResult> driveContentsCallback = new ResultCallback<DriveApi.DriveContentsResult>() {
@Override
public void onResult(DriveApi.DriveContentsResult result) {
if (result.getStatus().isSuccess()) {
OpenFileFromGoogleDrive();
}
}
};
public void OpenFileFromGoogleDrive() {
String EXISTING_FILE_ID = "1Zv5B9Fhh78bf7vavB1R_x-w3rbjezh28HPfjzjHVQww"; //My File ID
Drive.DriveApi.fetchDriveId(mGoogleApiClient, EXISTING_FILE_ID).setResultCallback(idCallback);
}
final private ResultCallback<DriveApi.DriveIdResult> idCallback = new ResultCallback<DriveApi.DriveIdResult>() {
@Override
public void onResult(DriveApi.DriveIdResult result) {
if (!result.getStatus().isSuccess()) {
System.out.println("Cannot find DriveId. Are you authorized to view this file?");
return;
}
DriveId driveId = result.getDriveId();
DriveFile file = driveId.asDriveFile();
new RetrieveDriveFileContentsAsyncTask(mGoogleApiClient).execute(file);
}
};
final private class RetrieveDriveFileContentsAsyncTask extends ApiClientAsyncTask<DriveFile, Boolean, String> {
RetrieveDriveFileContentsAsyncTask(GoogleApiClient mGoogleApiClient) {
super(mGoogleApiClient);
}
@Override
protected String doInBackgroundConnected(DriveFile... params) {
String contents = null;
DriveFile file = params[0];
DriveApi.DriveContentsResult driveContentsResult = file.open(getGoogleApiClient(), DriveFile.MODE_READ_ONLY, null).await(); `// Exception here`
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("DRIVE DATA", "IOException while reading from the stream", e);
}
driveContents.discard(getGoogleApiClient());
return contents;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (result == null) { **// Then resulting in null here**
System.out.println("Error while reading from the file");
return;
}
System.out.println("File contents: " + result);
}
}
ApiClientAsyncTask
public abstract class ApiClientAsyncTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {
private GoogleApiClient mClient;
public ApiClientAsyncTask(GoogleApiClient mClient) {
this.mClient = mClient;
}
@Override
protected final Result doInBackground(Params... params) {
Log.d("TAG", "in background");
final CountDownLatch latch = new CountDownLatch(1);
mClient.registerConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnectionSuspended(int cause) {
}
@Override
public void onConnected(Bundle arg0) {
latch.countDown();
}
});
mClient.registerConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult arg0) {
latch.countDown();
}
});
mClient.connect();
try {
latch.await();
} catch (InterruptedException e) {
return null;
}
if (!mClient.isConnected()) {
return null;
}
try {
return doInBackgroundConnected(params);
} finally {
mClient.disconnect();
}
}
protected abstract Result doInBackgroundConnected(Params... params);
protected GoogleApiClient getGoogleApiClient() {
return mClient;
}
}
我正在使用的EXISTING_FILE_ID是正确的,因为我使用othere方法获取文件名,并且它正确显示文件名。
任何帮助都是值得的。感谢