因此,我正在使用 Google Drive快速入门教程来帮助我了解与 Google Drive 交互的基础知识,并且代码在运行 Android 2.3.6 的 Samsung Ace 2 上运行良好,因此我能够将文件上传到我的 Google Drive 空间。我的问题是,当我在运行 Android 4.2.2 的三星 SII 上运行相同的应用程序时,它会找到该帐户并允许我创建一个新文件进行上传,但在以下情况下崩溃:
startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
LogCat 给了我以下信息:
03-26 18:29:39.686: E/AndroidRuntime(6775): FATAL EXCEPTION: Thread-256
03-26 18:29:39.686: E/AndroidRuntime(6775): java.lang.NullPointerException
03-26 18:29:39.686: E/AndroidRuntime(6775): at android.app.Activity.startActivityForResult(Activity.java:3370)
03-26 18:29:39.686: E/AndroidRuntime(6775): at android.app.Activity.startActivityForResult(Activity.java:3331)
03-26 18:29:39.686: E/AndroidRuntime(6775): at com.blitz_labs.Google$3.run(Google.java:209)
03-26 18:29:39.686: E/AndroidRuntime(6775): at java.lang.Thread.run(Thread.java:856)
相关代码
public void saveFileToDrive(final String fileName) {
Thread folder = new Thread(new Runnable() {
@Override
public void run() {
try {
Files.List request = service.files().list().setQ("mimeType='application/vnd.google-apps.folder' and trashed=false");
final FileList files = request.execute();
request.setPageToken(files.getNextPageToken());
if (!files.getItems().isEmpty()){
for(int i=0; i < files.getItems().size(); i++){
if(files.getItems().get(i).getTitle().equals("APP FOLDER")){
parentKind = files.getItems().get(i).getKind();
parentID = files.getItems().get(i).getId();
}
}
}else{
File body = new File();
body.setTitle("APP FOLDER");
body.setDescription("App folder");
body.setMimeType("application/vnd.google-apps.folder");
// service is an authorized Drive API service instance
File file = service.files().insert(body).execute();
parentKind = file.getKind();
parentID = file.getId();
driveFile = file;
Log.v("file EXISTS HERE:",""+driveFile.getTitle());
}
String mediaStorageDir = "/sdcard/APP FOLDER/";
fileUri = Uri.fromFile(new java.io.File(mediaStorageDir +java.io.File.separator + fileName));
// File's binary content
java.io.File fileContent = new java.io.File(fileUri.getPath());
FileContent mediaContent = new FileContent("text/plain", fileContent);
// File's metadata.
File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType("text/plain");
body.setParents(Arrays.asList(new ParentReference().setId(parentID) ));
final File file1 = service.files().insert(body, mediaContent).execute();
if (file1 != null) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext() , "file uploaded: " + file.getTitle(), Toast.LENGTH_SHORT).show();
Log.v("SUCESSFULL UPLOADTO GOOGLEDRIVE",""+file1.getTitle());
}
});
}
} catch (UserRecoverableAuthIOException e) {
startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
} catch (IOException e) {
e.printStackTrace();
}
}
});
folder.start();
}
有没有人对可能导致这种情况的原因有任何想法?
我已经包含了生成错误的文件上传功能。
预先感谢您的帮助。