I'm trying to use google Drive in my android project, I succeed to download one file from the drive, but not succeed to get a list of files from Google drive, I've worked for 2 weeks but I can't find a solution
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();
}
}
}
Assuming you've already tried your luck on this and this demo, you may also try a piece of code I put on Github here . It has all 5 basic (S)CRUD (Search, Create, Read, Update, Delete) functions for both GDAA and REST apis. If you manage to make it work in you environment, it should give you all the basic building blocks (including authentication and account switching) you need.
And bear in mind that since GDAA supports only FILE scope, you will not see files your Android App did not create. You have to use the REST api with DRIVE scope to search for files/folders created by other apps (ie web Drive).
Good Luck
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.