我正在做一个项目,我正在测试实现 Google Drive SDK,但是在尝试在 SD 卡上上传文件时遇到问题。
上传完成后出现错误
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)
下面是我用来执行上传的代码
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();
}
我相信崩溃发生在以下行:
com.google.api.services.drive.model.File file = service.files().insert(body, mediaContent).execute();
我不明白为什么会发生这种情况,并且该错误并不过分有用。我不是 Null 作为fileContent
andmediaContent
并且fileUri
不为 null 的东西。
它成功询问我要使用哪个帐户,并询问我是否要授予上传权限,因此我看不出有什么问题。
感谢您的任何帮助,您可以提供。
仅供参考,我使用了来自https://developers.google.com/drive/quickstart-android#step_2_enable_the_drive_api的教程