You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android端上传至Google Drive:取消弹窗直接上传的实现方法问询

Solution for Silent Google Drive File Upload on Android

Hey there! I get your frustration with that persistent upload picker popup—let's fix this so you can upload files directly without user interaction.

Why the Popup Appears

That popup comes from using Drive's default UI picker or intent-based upload flow, which is designed to let users choose where to save files. To skip this, you need to use the Drive API (via Google Play Services) to perform backend file creation instead of relying on UI components.

Is Drive API v3's Integrate-Create Doc Relevant?

Absolutely! That documentation covers exactly the kind of silent, programmatic file creation you need. It applies to Android as long as you use the correct client libraries to interact with the API.

Step-by-Step Fix

1. Update Your Dependencies

Your top-level build.gradle looks okay, but you need to add the Drive Play Services dependency to your module-level build.gradle (usually app/build.gradle):

dependencies {
    // Keep your existing dependencies
    implementation 'com.google.android.gms:play-services-drive:17.0.0' // Use a version compatible with your Gradle plugin
}

Make sure your Google Cloud project is set up with the Drive API enabled, and you've added the google-services.json file to your app module (from the Cloud Console).

2. Implement Silent Upload Code

Replace your current upload logic with this flow to create files directly:

  • First, authenticate the user (you probably already have this, but ensure you request the right scope, e.g., DriveScopes.DRIVE_FILE):
GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestScopes(new Scope(DriveScopes.DRIVE_FILE))
        .requestEmail()
        .build();
GoogleSignInClient client = GoogleSignIn.getClient(context, signInOptions);
// Launch sign-in if the user isn't already authenticated
  • Once authenticated, create the file silently:
// Get the authenticated account
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(context);
DriveClient driveClient = Drive.getDriveClient(context, account);
DriveResourceClient driveResourceClient = Drive.getDriveResourceClient(context, account);

// Define file metadata (set parent folder, name, MIME type, etc.)
FileMetadataChangeSet metadataChangeSet = new FileMetadataChangeSet.Builder()
        .setTitle("YourFileName.txt")
        .setMimeType("text/plain")
        // Optional: Set a parent folder ID to upload to a specific location
        // .setParents(Collections.singletonList("parent-folder-id"))
        .build();

// Create an output stream to write your file content
final Task<DriveContents> createContentsTask = driveResourceClient.createContents();
createContentsTask.continueWithTask(task -> {
    DriveContents contents = task.getResult();
    // Write your file data to the contents' output stream
    try (OutputStream outputStream = contents.getOutputStream()) {
        outputStream.write("Your file content here".getBytes());
    } catch (IOException e) {
        // Handle error
    }
    // Create the file on Drive
    return driveResourceClient.createFile(metadataChangeSet, contents);
}).addOnSuccessListener(driveFile -> {
    // File uploaded successfully!
}).addOnFailureListener(e -> {
    // Handle upload failure
});

Key Notes

  • Permissions: Ensure you request the appropriate scope (DRIVE_FILE is recommended for limited access, or DRIVE for full access if needed).
  • No UI: This code runs entirely in the background—no picker popup will appear because you're explicitly defining the file's metadata and location.
  • Drive API v3 Alignment: The integrate-create doc's principles apply here: you're programmatically defining the file's properties and content instead of relying on user input via the picker.

内容的提问来源于stack exchange,提问作者Yes92

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 10:12:37