title: How to download file from Google Drive with link in Android?
body: <p>I want to download file from google drive for which I am using dependency that is <code>compile 'com.google.android.gms:play-services:8.4.0'</code> and using this I am able to get link from meta data from below example.</p>
  
  <pre><code>mFileId = (DriveId) data.getParcelableExtra(
                          OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);
  
  
                  final DriveFile file = Drive.DriveApi.getFile(mGoogleApiClient, mFileId);
  
  
                  new Thread(new Runnable() {
                      @Override
                      public void run() {
                          // DO your work here
                          DriveResource.MetadataResult mdRslt = file.getMetadata(mGoogleApiClient).await();
                          if (mdRslt != null &amp;&amp; mdRslt.getStatus().isSuccess()) {
                             String  link = mdRslt.getMetadata().getWebContentLink();
                              String name=mdRslt.getMetadata().getTitle();
                              Log.d("LINK", link);
                              if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M &amp;&amp; getApplication().checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
                              {
                                  requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
                              }
                              else
                              {
  
                                new  get_download_data(link,name).execute();
                              }
                          }
                      }
                  }).start();
                  }
  </code></pre>
  
  <p>After getting the link from Google Drive, I am calling async task to download that file from link. So my problem is when I am downloading file, it's not opening. After checking and debugging, I found that my files was not downloading properly. </p>
  
  <p>For example, I have file name <code>abc.pdf</code> and the size is 400kb. I downloaded on my sdcard but <code>abc.pdf</code> is 56 kb only. I am using below code for downloading. I don't know where I was doing wrong. Please help. Thanks. </p>
  
  <pre><code> public class get_download_data extends AsyncTask&lt;Void,Void,String&gt;
  {
      File apkStorage = null;
      File outputFile = null;
      String link1="";
      String name1="";
  
      public get_database_data(String link, String name) {
          this.link1=link;
          this.name1=name;
      }
      protected void onPreExecute() {
          super.onPreExecute();
  
  
  
      }
      @Override
      protected String doInBackground(Void... params) {
  
          try {
  
              URL url = new URL(link1);//Create Download URl
  
              HttpURLConnection c = (HttpURLConnection) url.openConnection();//Open Url Connection
              c.setRequestMethod("GET");//Set Request Method to "GET" since we are grtting data
  
  
              c.setDoInput(true);
  
              c.connect();//connect the URL Connection
  
              //If Connection response is not OK then show Logs
              if (c.getResponseCode() != HttpURLConnection.HTTP_OK) {
                  Log.e(TAG, "Server returned HTTP " + c.getResponseCode()
                          + " " + c.getResponseMessage());
  
              }else{
                  Log.e(TAG, "Server returned HTTP " + c.getResponseCode()
                          + " " + c.getResponseMessage());
              }
  
  
              //Get File if SD card is present
              if (new CheckForSDCard().isSDCardPresent()) {
  
                  apkStorage = new File(
                          Environment.getExternalStorageDirectory() + "/"
                                  + "checkdb");
              } else
                  Toast.makeText(getApplication(), "Oops!! There is no SD Card.", Toast.LENGTH_SHORT).show();
  
              //If File is not present create directory
              if (!apkStorage.exists()) {
                  apkStorage.mkdir();
                  Log.e(TAG, "Directory Created.");
              }
  
              outputFile = new File(apkStorage, name1);//Create Output file in Main File
  
              //Create New File if not present
              if (!outputFile.exists()) {
                  outputFile.createNewFile();
                  Log.e(TAG, "File Created");
              }
  
              OutputStream fos = new FileOutputStream(outputFile);//Get OutputStream for NewFile Location
  
              InputStream is = c.getInputStream();//Get InputStream for connection
              BufferedInputStream inStream = new BufferedInputStream(is, 1024);
  
              byte[] buffer = new byte[1024];//Set buffer type
              int len1 = 0;//init length
              while ((len1 = inStream.read(buffer)) != -1) {
                  fos.write(buffer, 0, len1);//Write new file
              }
  
              //Close all connection after doing task
              fos.flush();
              fos.close();
              is.close();
  
          } catch (Exception e) {
  
              //Read exception if something went wrong
              e.printStackTrace();
              outputFile = null;
              Log.e(TAG, "Download Error Exception " + e.getMessage());
          }
          return null;
      }
  
      @Override
      protected void onPostExecute(String result_1) {
          super.onPostExecute(result_1);
          String downlodepath = Environment.getExternalStorageState()+"/"+name1;
          Log.e("Sdpath",""+imagePath);
          Toast.makeText(getApplication(), "download"+downlodepath, Toast.LENGTH_SHORT).show();
  
      }
  }
  </code></pre>
  
  <p>I found this link <a href="https://developers.google.com/drive/v3/web/manage-downloads#examples" rel="nofollow noreferrer">here</a> but some how I don't have idea how to implement this. Please let me know where I was wrong. Thanks.</p>