0

我正在开发一个 android 应用程序,我正在尝试将 Google Drive 集成到我的应用程序中。该程序允许同步,因此它会在 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();
    }

以下是我如何获得驱动服务

private Drive getDriveService(GoogleAccountCredential credential) { return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential).build(); }

下面是从异常输出的堆栈跟踪

> 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)
4

1 回答 1

1

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

于 2013-03-24T00:06:48.017 回答