0

I tried to create a spreadsheet in Android. It sent a success message but it wasn't created.

I used the following code to create it:

MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
.setMimeType("application/vnd.google-apps.spreadsheet").setTitle("MonthExpense").build();
IntentSender intentSender = Drive.DriveApi
.newCreateFileActivityBuilder()
.setInitialMetadata(metadataChangeSet)
.setInitialDriveContents(result.getDriveContents())
.build(appobject.mGoogleApiClient);
try {
activity.startIntentSenderForResult(intentSender, 56, null, 0, 0, 0);
}

If I use any other mimetype it works correctly can anyone please guide me in the right way.

Edit If i try any other mime type it works with out problem, So i guess the problem related with "application/vnd.google-apps.spreadsheet"

Rakki s
  • 1,426
  • 1
  • 18
  • 42
  • Probably the problem is that you are trying to upload Google spreadsheet without any body or maybe you should have it working by using the pure Java Drive SDK. – Dayton Wang Feb 05 '15 at 00:39
  • Can you suggest me how to add body in it. – Rakki s Feb 05 '15 at 04:45
  • I have try with java sdk too but it returns 400 error – Rakki s Feb 05 '15 at 04:46
  • See the reference on these links: http://stackoverflow.com/questions/21569851/how-do-i-upload-file-to-google-drive-from-android, https://developers.google.com/drive/android/create-file – Dayton Wang Feb 05 '15 at 17:40
  • refer this link https://stackoverflow.com/a/49297056/1201441, there describe upload file in google drive. you can upload any type of file just change file_path and respected Mime type – TejaDroid Mar 15 '18 at 10:50

1 Answers1

0

Try to change application/vnd.google-apps.spreadsheet to application/vnd.ms-excel

Create a WorkBook. This is the end of my working code

final private ResultCallback<DriveContentsResult> driveContentsCallback = new ResultCallback<DriveContentsResult>() {
    @Override
    public void onResult(final DriveContentsResult result) {
        if (!result.getStatus().isSuccess()) {
            showMessage("Error while trying to create new file contents");
            return;
        }
        final DriveContents driveContents = result.getDriveContents();

        // Perform I/O off the UI thread.
        new Thread() {
            @Override
            public void run() {

 //CREATE YOUR WORKBOOK HERE
ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    try {
                        wb.write(bos);
                        byte[] bytes = bos.toByteArray();
                        outputStream.write(bytes);
                        //WORKBOOK FILENAME
                        writer.write(fileName);
                        wb.close();
                    } catch (IOException e) {
                        Log.e(TAG, e.getMessage());
                    }

                    MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                            .setTitle(fileName)
                            .setMimeType("application/vnd.ms-excel")
                            .setStarred(true).build();

                    // create a file on root folder
                    Drive.DriveApi
                            .getRootFolder(getGoogleApiClient())
                            .createFile(getGoogleApiClient(), changeSet,
                                    driveContents)
                            .setResultCallback(fileCallback);
SmulianJulian
  • 801
  • 11
  • 33
  • After creating as MS-excel. is it possible to use via spread sheet api ? – Rakki s Feb 09 '15 at 10:23
  • I don't see why not. I took a quick look at the Google Sheets api and I didn't see anything in there to stop it. Just fyi, I am able to open this file on Google Sheets with ease. – SmulianJulian Feb 09 '15 at 11:26
  • ok thanks for the information, I will try to implement that and get back you soon. – Rakki s Feb 09 '15 at 12:22