Boardy
Boardy

Reputation: 36205

Google Drive Android SDK keeps crashing on file upload

I am working on a project and I am doing a test on implementing the Google Drive SDK but I am having a problem when trying to upload a file on the SD card.

When the upload is done I am getting an error

02-23 23:38:54.960: E/AndroidRuntime(9160): FATAL EXCEPTION: Thread-2652
02-23 23:38:54.960: E/AndroidRuntime(9160): java.lang.NullPointerException
02-23 23:38:54.960: E/AndroidRuntime(9160):     at java.net.URI.parseURI(URI.java:353)
02-23 23:38:54.960: E/AndroidRuntime(9160):     at java.net.URI.<init>(URI.java:204)
02-23 23:38:54.960: E/AndroidRuntime(9160):     at com.google.api.client.http.GenericUrl.<init>(GenericUrl.java:100)
02-23 23:38:54.960: E/AndroidRuntime(9160):     at com.google.api.client.googleapis.media.MediaHttpUploader.upload(MediaHttpUploader.java:269)
02-23 23:38:54.960: E/AndroidRuntime(9160):     at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:408)
02-23 23:38:54.960: E/AndroidRuntime(9160):     at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:328)
02-23 23:38:54.960: E/AndroidRuntime(9160):     at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:449)
02-23 23:38:54.960: E/AndroidRuntime(9160):     at com.BoardiesITSolutions.googledrivetest.MainActivity$3.run(MainActivity.java:127)
02-23 23:38:54.960: E/AndroidRuntime(9160):     at java.lang.Thread.run(Thread.java:856)

Below is the code I am using to perform the upload

private void saveFileToDrive()
    { 
        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
                try
                {
                    String storageDir = Environment.getExternalStorageDirectory().getPath();
                    fileUri = Uri.fromFile(new java.io.File(storageDir + "/BoardiesPasswordManager/android_download.xml"));
                    java.io.File fileContent = new java.io.File(fileUri.getPath());
                    FileContent mediaContent = new FileContent("text/xml", fileContent);

                    com.google.api.services.drive.model.File body = new com.google.api.services.drive.model.File();
                    body.setTitle("Boardies Password Manager");
                    body.setMimeType("text/xml");

                    com.google.api.services.drive.model.File file = service.files().insert(body, mediaContent).execute();
                    //Update file = service.files().update(body.getId(), body, mediaContent);
                    if (file != null)
                    {
                        //Toast.makeText(MainActivity.this, "File Uploaded: " + file.getTitle(), Toast.LENGTH_LONG).show();
                        Log.d("GoogleDrive", "File Uploaded");
                    }

                }
                catch (UserRecoverableAuthIOException e)
                {
                    startActivityForResult(e.getIntent(), REQUEST_AUTHORISATION);
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
    }

I believe the crash is occurring on the following line:

com.google.api.services.drive.model.File file = service.files().insert(body, mediaContent).execute();

I don't see why this is happening and the error isn't overly useful. I'm not what it is that is Null as the fileContent and mediaContent and fileUri are not null.

It successfully asks which account I want to use and asks if I want to give permission for the upload so I can't see what's wrong.

Thanks for any help you can provide.

FYI I have used the tutorial from https://developers.google.com/drive/quickstart-android#step_2_enable_the_drive_api

Upvotes: 2

Views: 1732

Answers (2)

Boardy
Boardy

Reputation: 36205

I've managed to find the problem but Edward van Raak did help.

As suggested part of the problem was to do with the different dev PC I was using and I'd forgotten to add a different client SHA1 fingerprint to the API console. However, this didn't resolve the issue as the same exception was thrown.

I then removed the Google Play Services jar file and removed the Google Drive API (note you will get build errors now).

I then re-added the google play services jar file and re-added the google drive API using the Google API Eclipse plugin and re-built the project and I can now successfully upload a file again.

Upvotes: 1

Edward van Raak
Edward van Raak

Reputation: 4910

Not sure if I can help but it seems like a failed authorization problem coming from within the API not your code.

Check your credentials in the APIs Console, package name and SHA1 fingerprint and make sure they are correct. And are you using the latest version of the client library?

Upvotes: 0

Related Questions