title: How to download google drive files using android api
body: <p>The following program will display the "shared files with you" from google drive. i am displaying all the files in a listView, here i want to download the google drive file whenever a user clicks the list item and i have written a code to get file's drive id. </p>
  
  <p>This is what i have done in the following code. </p>
  
  <p>Please help me to download a file from google drive whenever a user clicks the list item using google drive android api. </p>
  
  <pre><code>public class MainActivity extends BaseDemoActivity{ 
  private static final String TAG = "MainActivity";
  private ListView mResultsListView;
  private ResultsAdapter mResultsAdapter;
  private ProgressBar mProgressBar;
  private DriveId mSelectedFileDriveId;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      mResultsListView = (ListView) findViewById(R.id.listViewSamples);
      mResultsAdapter = new ResultsAdapter(this);
      mResultsListView.setAdapter(mResultsAdapter);
  
      mResultsListView.setOnItemClickListener(new OnItemClickListener() {
  
          @Override
          public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                  int position, long id) {
                   if (mSelectedFileDriveId != null) {
                          open();
                          return;
                      }
              }
  
  
      });
  }
  private void open() {
      showMessage("Select files drive id = "+mSelectedFileDriveId);
      // Reset progress dialog back to zero as we're
      // initiating an opening request.
     // mProgressBar.setProgress(0);
      DownloadProgressListener listener = new DownloadProgressListener() {
          @Override
          public void onProgress(long bytesDownloaded, long bytesExpected) {
              // Update progress dialog with the latest progress.
              int progress = (int)(bytesDownloaded*100/bytesExpected);
              Log.d(TAG, String.format("Loading progress: %d percent", progress));
              mProgressBar.setProgress(progress);
          }
      };
  
      Drive.DriveApi.getFile(getGoogleApiClient(), mSelectedFileDriveId)
          .open(getGoogleApiClient(), DriveFile.MODE_READ_WRITE, listener)
          .setResultCallback(driveContentsCallback);
      mSelectedFileDriveId = null;
  
  }
  
  private ResultCallback&lt;DriveContentsResult&gt; driveContentsCallback =
          new ResultCallback&lt;DriveContentsResult&gt;() {
      @Override
      public void onResult(DriveContentsResult result) {
          if (!result.getStatus().isSuccess()) {
              showMessage("Error while opening the file contents");
              return;
          }
          showMessage("File contents opened");       
      }
  };
  
  /**
   * Clears the result buffer to avoid memory leaks as soon as the activity is no longer
   * visible by the user.
   */
  @Override
  protected void onStop() {
      super.onStop();
      mResultsAdapter.clear();
  }
  
  @Override
  public void onConnected(Bundle connectionHint) {
      super.onConnected(connectionHint); 
     Drive.DriveApi.requestSync(getGoogleApiClient())
     .setResultCallback(statusCallback);
      showMessage("Connecting ...");
  }
  
  final private ResultCallback&lt;Status&gt; statusCallback =
          new ResultCallback&lt;Status&gt;() {
              @Override
              public void onResult(Status arg0) {
                  // TODO Auto-generated method stub
               showMessage("Fetching...");
               if(arg0.getStatus().isSuccess()){
                   Query query = new Query.Builder()
                      .addFilter(Filters.sharedWithMe())
                      .build();
  
                   Drive.DriveApi.query(getGoogleApiClient(), query)
                   .setResultCallback(metadataCallback);
               }else{
                   showMessage("Error = "+arg0);
               }
          }
  };
  
  final private ResultCallback&lt;DriveApi.MetadataBufferResult&gt; metadataCallback = 
          new ResultCallback&lt;DriveApi.MetadataBufferResult&gt;() {
              @Override
              public void onResult(DriveApi.MetadataBufferResult result) {
                  // TODO Auto-generated method stub
                  showMessage("onResult ...");
                  if (!result.getStatus().isSuccess()) {
                      showMessage("Problem while retrieving results");
                      return;
                  }
                  mResultsAdapter.clear();
                  mResultsAdapter.append(result.getMetadataBuffer());
                  MetadataBuffer buffer = result.getMetadataBuffer();
                  Metadata meta = buffer.get(0);
                  Log.v(TAG,"WEB CONTENT LINK = "+meta.getWebContentLink());
                  mSelectedFileDriveId = (DriveId) meta.getDriveId();
                  showMessage("Count = "+mResultsAdapter.getCount());
              }
          };  
  </code></pre>
  
  <p>}</p>