将文件上传到Google云端硬盘会导致NullPointerException

时间:2013-03-23 17:11:39

标签: android google-drive-api

我正在开发Android应用,我正在尝试将Google云端硬盘集成到我的应用中。该程序允许同步,因此它在SD卡上创建一个文件,然后将该文件上传到Google Drive。我正在成功进行身份验证,但是当我使用insert函数上传时,我得到一个NullPointerException并且我无法看到值为null的位置,因为当我逐步执行所有操作时,似乎没有任何内容为null。

以下是我用来执行上传的代码。

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

            public void run() {
                try
                {
                    SharedPreferences settings = context.getSharedPreferences("prefs", 0);

                    GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(context, DriveScopes.DRIVE);

                    credential.setSelectedAccountName(settings.getString("google_drive_account_name", ""));
                    Drive service = getDriveService(credential);

                    String storageDir = Environment.getExternalStorageDirectory().getPath();
                    Uri fileUri = Uri.fromFile(new java.io.File(storageDir + "/folder/drive_sync.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("My App Title");
                    body.setMimeType("text/xml");

                    com.google.api.services.drive.model.File file = service.files().insert(body, mediaContent).execute();
                    if (file == null)
                    {
                        Log.e("UploadFile", "File was empty after insert");
                    }
                }
                catch (UserRecoverableAuthIOException e)
                {
                    activity.startActivityForResult(e.getIntent(), SynchronisationSettings.REQUEST_ACCOUNT_PICKER);
                }
                catch (IOException ex)
                {
                    Log.d("Upload Drive", ex.toString());
                    ex.printStackTrace();
                }
                catch (Exception ex)
                {
                    Log.d("Upload File", ex.toString());
                    ex.printStackTrace();
                }
            }
        });
        thread.start();
    }

以下是我获取云端服务的方式

私人云端硬盘getDriveService(GoogleAccountCredential凭据) {     返回新的Drive.Builder(AndroidHttp.newCompatibleTransport(),新的GsonFactory(),凭证).build(); }

以下是从Exception

输出的堆栈跟踪
> 03-23 17:05:00.050: W/System.err(21664):
> java.lang.NullPointerException 03-23 17:05:00.055:
> W/System.err(21664):  at java.net.URI.parseURI(URI.java:353) 03-23
> 17:05:00.055: W/System.err(21664):    at
> java.net.URI.<init>(URI.java:204) 03-23 17:05:00.060:
> W/System.err(21664):  at
> com.google.api.client.http.GenericUrl.<init>(GenericUrl.java:100)
> 03-23 17:05:00.065: W/System.err(21664):  at
> com.google.api.client.googleapis.media.MediaHttpUploader.upload(MediaHttpUploader.java:269)
> 03-23 17:05:00.065: W/System.err(21664):  at
> com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:408)
> 03-23 17:05:00.065: W/System.err(21664):  at
> com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:328)
> 03-23 17:05:00.070: W/System.err(21664):  at
> com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:449)
> 03-23 17:05:00.070: W/System.err(21664):  at
> com.BoardiesITSolutions.PasswordManager.Classes.DriveSync$1.run(DriveSync.java:85)
> 03-23 17:05:00.070: W/System.err(21664):  at
> java.lang.Thread.run(Thread.java:856)

1 个答案:

答案 0 :(得分:1)

我找到了原因,这是因为我使用的是另一台开发PC而忘记添加SHA1指纹并添加到Drive API的开发人员API控制台。