title: Uploading an Image to Google Drive Using Drive API
body: <p>I am trying to upload an image to the google drive using drive API. As the first step, I developed a code to upload a PDF file (file path is hard coded) to the google drive by following a tutorial.</p>
  <ol>
  <li><p>PDF will be uploaded successfully but sometimes I get a time out message from the log. this may be because of my poor connection. But is there a proper way to handle this by the code when something like this happens?</p>
  </li>
  <li><p>Please guide me on how to upload an image instead of PDF by changing this code. I tried but I could not do it successfully. <strong>Please Help for this part. I am stuck here for two days</strong></p>
  </li>
  </ol>
  <pre><code>import android.util.Log;
  import com.google.android.gms.tasks.Task;
  import com.google.android.gms.tasks.Tasks;
  import com.google.api.client.http.FileContent;
  import com.google.api.services.drive.Drive;
  import com.google.api.services.drive.model.File;
  
  import java.io.IOException;
  import java.util.concurrent.Executor;
  import java.util.concurrent.Executors;
  
  public class DriveServiceHelper {
  
      private final Executor mExecutor = Executors.newSingleThreadExecutor();
      private Drive mDriveService;
  
      public DriveServiceHelper(Drive mDriveService){
  
          this.mDriveService = mDriveService;
      }
  
      public Task&lt;String&gt; createFilePdf(String filePath){
          return Tasks.call(mExecutor, () -&gt; {
              File fileMetaData = new File();
              fileMetaData.setName(&quot;MyPDFFile&quot;);
  
              java.io.File file = new java.io.File(filePath);
  
              FileContent mediaContent = new FileContent(&quot;application/pdf&quot;,file);
  
              File myFile = null;
              try {
                  myFile = mDriveService.files().create(fileMetaData,mediaContent).execute();
              }catch (Exception e){
                  e.printStackTrace();
                  Log.i(&quot;myissue&quot;, e.getMessage());
              }
  
              if (myFile == null){
                  throw new IOException(&quot;Null result when requesting file creation&quot;);
              }
              return myFile.getId();
  
  
          });
      }
  }
  </code></pre>
  <pre><code>
  
  import androidx.annotation.NonNull;
  import androidx.annotation.Nullable;
  import androidx.appcompat.app.AppCompatActivity;
  
  import android.app.ProgressDialog;
  import android.content.Intent;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Toast;
  
  import com.google.android.gms.auth.api.signin.GoogleSignIn;
  import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
  import com.google.android.gms.auth.api.signin.GoogleSignInClient;
  import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
  import com.google.android.gms.common.api.Scope;
  import com.google.android.gms.tasks.OnFailureListener;
  import com.google.android.gms.tasks.OnSuccessListener;
  import com.google.api.client.extensions.android.http.AndroidHttp;
  import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
  import com.google.api.client.json.gson.GsonFactory;
  import com.google.api.services.drive.Drive;
  import com.google.api.services.drive.DriveScopes;
  
  import java.util.Collections;
  
  public class MainActivity extends AppCompatActivity {
  
      DriveServiceHelper driveServiceHelper;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          
          requestSignIn();
      }
  
      private void requestSignIn() {
          GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                  .requestEmail()
                  .requestScopes(new Scope(DriveScopes.DRIVE_FILE))
                  .build();
  
          GoogleSignInClient client = GoogleSignIn.getClient(this,signInOptions);
  
          startActivityForResult(client.getSignInIntent(),400);
      }
  
      @Override
      protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
  
          switch (requestCode){
              case 400:
                  if (resultCode == RESULT_OK){
                      handleSignInIntent(data);
                      break;
                  }
          }
      }
  
      private void handleSignInIntent(Intent data) {
          GoogleSignIn.getSignedInAccountFromIntent(data)
                  .addOnSuccessListener(new OnSuccessListener&lt;GoogleSignInAccount&gt;() {
                      @Override
                      public void onSuccess(GoogleSignInAccount googleSignInAccount) {
                          GoogleAccountCredential credential = GoogleAccountCredential
                                  .usingOAuth2(MainActivity.this, Collections.singleton(DriveScopes.DRIVE_FILE));
  
                          credential.setSelectedAccount(googleSignInAccount.getAccount());
  
                          Drive googleDriveServices = new Drive.Builder(
                                  AndroidHttp.newCompatibleTransport(),
                                  new GsonFactory(),
                                  credential)
                                  .setApplicationName(&quot;uploadtodrive&quot;)
                                  .build();
  
                          driveServiceHelper = new DriveServiceHelper(googleDriveServices);
  
                      }
                  })
                  .addOnFailureListener(new OnFailureListener() {
                      @Override
                      public void onFailure(@NonNull Exception e) {
  
                      }
                  });
      }
  
      public void uploadPdfFile(View v){
  
          ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
          progressDialog.setTitle(&quot;Uploading to google Drive&quot;);
          progressDialog.setMessage(&quot;Please wait........&quot;);
          progressDialog.show();
  
          String filePath = &quot;/storage/emulated/0/mypdf.pdf&quot;;
  
          driveServiceHelper.createFilePdf(filePath).addOnSuccessListener(new OnSuccessListener&lt;String&gt;() {
              @Override
              public void onSuccess(String s) {
                  progressDialog.dismiss();
                  Toast.makeText(getApplicationContext(),&quot;Uploaded Successfully&quot;, Toast.LENGTH_SHORT).show();
              }
          })
                  .addOnFailureListener(new OnFailureListener() {
                      @Override
                      public void onFailure(@NonNull Exception e) {
                          progressDialog.dismiss();
                          Toast.makeText(getApplicationContext(),&quot;Check your google Drive api key&quot;,Toast.LENGTH_SHORT).show();
                      }
                  });
  
  
  
      }
  }
  </code></pre>
  <p><strong>Please Help for this part. I am stuck here for two days</strong>. thank you</p>