2012-09-18 48 views
1

我已经构建了一个Android应用,我使用相机拍照,然后尝试将照片上传到我的Google云端硬盘帐户。下面是我使用的代码片段:从Android应用上传jpeg到Google Drive“流量意外结束”

HttpTransport httpTransport = new NetHttpTransport(); 
    JacksonFactory jsonFactory = new JacksonFactory(); 
    Drive.Builder b = new Drive.Builder(httpTransport, jsonFactory, null); 
    b.setJsonHttpRequestInitializer(new JsonHttpRequestInitializer() { 
     public void initialize(JsonHttpRequest request) throws IOException { 
      DriveRequest driveRequest = (DriveRequest) request; 
      driveRequest.setPrettyPrint(true); 
      driveRequest.setKey(MY_CLIENT_ID); 
      driveRequest.setOauthToken(token); 
     } 
    }); 

    final Drive drive = b.build(); 


    final com.google.api.services.drive.model.File body = new com.google.api.services.drive.model.File(); 
    body.setTitle(mCurrentPhotoPath.substring(mCurrentPhotoPath.lastIndexOf("/") + 1, mCurrentPhotoPath.length())); 
    body.setDescription("A Test File"); 
    body.setMimeType("image/jpeg"); 

    java.io.File file = new java.io.File(mCurrentPhotoPath); 

    final FileContent mediaContent = new FileContent("image/jpeg", file); 
    //final InputStreamContent mediaContent = new InputStreamContent("image/jpeg", new FileInputStream(file)); 

    new Thread(new Runnable() { 
     public void run() { 
      try { 
       com.google.api.services.drive.model.File file = drive.files().insert(body, mediaContent).execute(); 

       System.out.println("Title: " + file.getTitle()); 
       System.out.println("Description: " + file.getDescription()); 
       System.out.println("MIME type: " + file.getMimeType()); 
      } 
      catch (GoogleJsonResponseException e) { 
       GoogleJsonError error = e.getDetails(); 

       System.err.println("Error code:" + error.getCode()); 
       System.err.println("Error message: " + error.getMessage()); 

       // More error information can be retrieved with error.getErrors(). 
      } 
      catch (HttpResponseException e) { 
       // No Json body was returned by the API. 
       System.err.println("HTTP Status code: " + e.getStatusCode()); 
       System.err.println("HTTP Reason: " + e.getMessage()); 
      } 
      catch (IOException e) { 
       // Other errors (e.g connection timeout, etc.). 
       System.out.println("An error occurred: " + e); 
      } 
     } 
    }).start(); 

发生了什么事是我得到的logcat的下列情况除外:

09-17 22:11:31.683: WARN/HttpTransport(11701): exception thrown while executing request 
    java.io.IOException: unexpected end of stream 
    at libcore.net.http.FixedLengthOutputStream.close(FixedLengthOutputStream.java:58) 
    at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:88) 
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:991) 
    at com.google.api.client.googleapis.media.MediaHttpUploader.upload(MediaHttpUploader.java:279) 
    at com.google.api.services.drive.Drive$Files$Insert.executeUnparsed(Drive.java:309) 
    at com.google.api.services.drive.Drive$Files$Insert.execute(Drive.java:331) 
    at com.xxxxxx.UploadImageService$2.run(UploadImageService.java:143) 
    at java.lang.Thread.run(Thread.java:856) 

当我通过谷歌驱动器加强和客户端代码看看它在做什么,它实际上似乎工作两次。但现在它又失败了。

任何想法?

回答

0

我想我可能已经想通了我的问题。奇怪的是我从驱动api中得到的错误,但最终,我想我正试图从相机捕捉图像,并保存到第二个时间戳。我在文件名中添加了毫秒,到目前为止我还没有看到异常。我们拭目以待。