2017-05-30 236 views
0

我試圖以編程方式將現有的.vcf文件上傳到Google Drive。到目前爲止它只保存一個空文件。使用Google Drive API上傳文件

這可能很簡單,但我不熟悉Google Drive Api。 下面是代碼:

// Perform I/O off the UI thread. 
    new Thread() { 
     @Override 
     public void run() { 
      // write content to DriveContents 
      File f = new File("גיבוי אנשי קשר" + ".vcf"); 
      filepath = f.getAbsolutePath().toString(); 


      //String inFileName = Environment.getExternalStorageDirectory().getPath() + "גיבוי אנשי קשר" + ".vcf" ; 
      String inFileName = filepath; 
      File backupFile = new File(inFileName); 
      FileInputStream inputStream = null; 
      try { 
       inputStream = new FileInputStream(backupFile); 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } 
      BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); 
      byte[] buffer = new byte[8 * 1024]; 

      BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(driveContents.getOutputStream()); 
      int n = 0; 
      try { 
       while ((n = bufferedInputStream.read(buffer)) > 0) { 
        bufferedOutputStream.write(buffer, 0, n); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      try { 
       bufferedInputStream.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      //final MimeTypeMap mime = MimeTypeMap.getSingleton(); 
      // String tmptype = mime.getMimeTypeFromExtension("vcf"); 
      MetadataChangeSet changeSet = new MetadataChangeSet.Builder() 
        .setTitle("גיבוי אנשי קשר") 
        .setMimeType("text/x-vcard") 
        .setStarred(true).build(); 

回答

1

檢查Uploading Files在那裏你將使用Files.create上傳的文件。

請務必指定uploadType,因爲它是必需的參數。

下面是從文檔的片段:

File fileMetadata = new File(); 
fileMetadata.setName("photo.jpg"); 
java.io.File filePath = new java.io.File("files/photo.jpg"); 
FileContent mediaContent = new FileContent("image/jpeg", filePath); 
File file = driveService.files().create(fileMetadata, mediaContent) 
     .setFields("id") 
     .execute(); 
System.out.println("File ID: " + file.getId()); 
+0

這個例子是不相關的我也跟着在GitHub上的谷歌API的例子,這是什麼跟了上來。你能用我的代碼想到任何解決方案嗎? –

相關問題