Я использую Google Drive API в своем приложении, я могу выбрать (в моем случае .doc/docx/.pdf) файл с Google Диска, до сих пор все в порядке. Но я хочу загрузить выбранный файл, и мне нужно отправить его на наш сервер с помощью Multipart. Я попробовал несколько способов: получаю DriveId и DriveFile, но, к сожалению, не могу загрузить выбранный файл.
Я просмотрел документацию для разработчиков Android
Я использую Google [b]Drive API[/b] в своем приложении, я могу выбрать (в моем случае .doc/docx/.pdf) файл с Google Диска, до сих пор все в порядке. Но я хочу загрузить выбранный файл, и мне нужно отправить его на наш сервер с помощью Multipart. Я попробовал несколько способов: получаю DriveId и DriveFile, но, к сожалению, не могу загрузить выбранный файл.
Я просмотрел документацию для разработчиков Android
public class DriveActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { /** * DriveId of an existing folder to be used as a parent folder in * folder operations samples. */ public static final String EXISTING_FOLDER_ID = "0B2EEtIjPUdX6MERsWlYxN3J6RU0"; /** * DriveId of an existing file to be used in file operation samples.. */ public static final String EXISTING_FILE_ID = "0ByfSjdPVs9MZTHBmMVdSeWxaNTg"; /** * Extra for account name. */ protected static final String EXTRA_ACCOUNT_NAME = "account_name"; /** * Request code for auto Google Play Services error resolution. */ protected static final int REQUEST_CODE_RESOLUTION = 1; /** * Next available request code. */ protected static final int NEXT_AVAILABLE_REQUEST_CODE = 2; private static final String TAG = "===GoogleDriveActivity"; private static final int REQUEST_CODE_OPENER = 2; /** * Google API client. */ private GoogleApiClient mGoogleApiClient; private static final String[] SCOPES = {DriveScopes.DRIVE_FILE};
final HttpTransport transport = AndroidHttp.newCompatibleTransport(); final JsonFactory jsonFactory = GsonFactory.getDefaultInstance(); private String accountName; DriveResource.MetadataResult metadataResult;
private void connect() { if (mGoogleApiClient == null) { mGoogleApiClient = new GoogleApiClient.Builder(this) .addApi(Drive.API) .addScope(Drive.SCOPE_FILE) .addScope(Drive.SCOPE_APPFOLDER) // required for App Folder sample .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); } mGoogleApiClient.connect(); }
/** * Called when {@code mGoogleApiClient} is disconnected. */ @Override public void onConnectionSuspended(int cause) { Log.i(TAG, "GoogleApiClient connection suspended"); }
/** * Called when {@code mGoogleApiClient} is trying to connect but failed. * Handle {@code result.getResolution()} if there is a resolution is * available. */ @Override public void onConnectionFailed(ConnectionResult result) { Log.i(TAG, "GoogleApiClient connection failed: " + result.toString()); if (!result.hasResolution()) { // show the localized error dialog. GoogleApiAvailability.getInstance().getErrorDialog(this, result.getErrorCode(), 0).show(); return; } try { result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION); } catch (IntentSender.SendIntentException e) { Log.e(TAG, "Exception while starting resolution activity", e); } }
/** * Getter for the {@code GoogleApiClient}. */ public GoogleApiClient getGoogleApiClient() { return mGoogleApiClient; }
@Override public void onConnected(Bundle connectionHint) { IntentSender intentSender = Drive.DriveApi .newOpenFileActivityBuilder() .setMimeType(new String[]{"application/msword", " application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/vnd.google-apps.document", "application/pdf"}) .build(getGoogleApiClient()); AccountManager manager = (AccountManager) getSystemService(ACCOUNT_SERVICE); Account[] list = manager.getAccountsByType("com.google"); //Getting the first account because that is the primary account for that user accountName = list[0].name; try { startIntentSenderForResult( intentSender, REQUEST_CODE_OPENER, null, 0, 0, 0); } catch (IntentSender.SendIntentException e) { Log.w(TAG, "Unable to send intent", e); } }