我尝试在我的Android项目中使用谷歌云端硬盘,我成功从驱动器下载了一个文件,但没有成功从谷歌驱动器获取文件列表,我已经工作了2周但我无法找到解决方案
public class RetrieveContentsActivity extends Activity implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener {
/*get this id from your google drive on the web*/
private static final String EXISTING_FILE_ID = "0B1_qyX0EdGokaWVVSWxzd2hLdWc";
private static final int REQUEST_CODE = 102;
private GoogleApiClient googleApiClient;
private static final String TAG = "retrieve_contents";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_retrieve_contents);
/*build the api client*/
buildGoogleApiClient();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.i(TAG, "In onStart() - connecting...");
googleApiClient.connect();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
if (googleApiClient != null) {
Log.i(TAG, "In onStop() - disConnecting...");
googleApiClient.disconnect();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
Log.i(TAG, "In onActivityResult() - connecting...");
googleApiClient.connect();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.retrieve_contents, menu);
return true;
}
@Override
public void onConnected(Bundle bundle) {
// TODO Auto-generated method stub
Drive.DriveApi.fetchDriveId(googleApiClient, EXISTING_FILE_ID).setResultCallback(idCallback);
}
@Override
public void onConnectionSuspended(int arg0) {
// TODO Auto-generated method stub
Drive.DriveApi.fetchDriveId(googleApiClient, EXISTING_FILE_ID).setResultCallback(idCallback);
}
/*callback on getting the drive id, contained in result*/
final private ResultCallback<DriveIdResult> idCallback = new ResultCallback<DriveIdResult>() {
@Override
public void onResult(DriveIdResult result) {
DriveFile file = Drive.DriveApi.getFile(googleApiClient, result.getDriveId());
/*use a pending result to get the file contents */
PendingResult<ContentsResult> pendingResult = file.openContents(googleApiClient, DriveFile.MODE_READ_ONLY, null);
/*the callback receives the contents in the result*/
pendingResult.setResultCallback(new ResultCallback<ContentsResult>() {
public String fileAsString;
@Override
public void onResult(ContentsResult result) {
Contents fileContents = result.getContents();
InputStream iStream = fileContents.getInputStream();
Log.i(TAG, "reading input stream and building string...");
//Create folder To Save the File Downloaded
File root = new File(Environment.getExternalStorageDirectory(),"Téléchargement");
if (!root.exists()) {root.mkdirs();}
//Create File
File file = new File(root, "lena.jpg");
storeFile(file, iStream);
//fileContents.discard(googleApiClient);
Intent intent = new Intent(RetrieveContentsActivity.this, DisplayFileActivity.class);
intent.putExtra("text", fileAsString);
startActivity(intent);
}
});
}
};
/*callback when there there's an error connecting the client to the service.*/
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.i(TAG, "Connection failed");
if (!result.hasResolution()) {
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this, 0).show();
return;
}
try {
result.startResolutionForResult(this, REQUEST_CODE);
} catch (IntentSender.SendIntentException e) {
Log.e(TAG, "Exception while starting resolution activity", e);
}
}
/*build the google api client*/
private void buildGoogleApiClient() {
if (googleApiClient == null) {
googleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
}
private void storeFile(File file, InputStream iStream)
{
try
{
final OutputStream oStream = new FileOutputStream(file);
try
{
try
{
final byte[] buffer = new byte[1024];
int read;
while ((read = iStream.read(buffer)) != -1)
{
oStream.write(buffer, 0, read);
}
oStream.flush();
} finally {
oStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
假设您已经在this和this演示中尝试过运气,您也可以尝试我放在Github here上的一段代码。它具有GDAA和REST apis的所有5个基本(S)CRUD(搜索,创建,读取,更新,删除)功能。如果您设法使其在您的环境中工作,它应该为您提供所需的所有基本构建块(包括身份验证和帐户切换)。
请记住,由于GDAA仅支持FILE范围,因此您不会看到Android App未创建的文件。您必须使用带有DRIVE范围的REST api来搜索由其他应用程序(即Web驱动器)创建的文件/文件夹。
祝你好运