2016-05-15 190 views
0

我只是无法管理从谷歌驱动器下载文件,我刚刚创建。我可以获得webContentLink。如果我通过浏览器访问此文件,我可以下载文件,但是我无法通过我的应用程序下载相同的文件...我几乎阅读了有关谷歌驱动器的所有文档,但我找不到任何方式将文件下载到我的存储。使用google-api-services-drive从谷歌驱动器下载文件:v3

在这里,我得到的链接文件...我能得到我想要的东西永远是这样

downloadLink = m.getAlternateLink(); 
downloadLink = m.getEmbedLink(); 
downloadLink = m.getWebContentLink(); 
downloadLink = m.getWebViewLink(); 

或类似驱动器Id所有其他参数...

这里是我的代码来获得它的驱动器上的文件的信息:

Query query2 = new Query.Builder() 
    .addFilter(Filters.and(Filters.eq(
    SearchableField.TITLE, "locations.csv"), 
    Filters.eq(SearchableField.TRASHED, false))) 
           .build(); 
    Drive.DriveApi.query(mGoogleApiClient, query2) 
    .setResultCallback(new ResultCallback<DriveApi.MetadataBufferResult>() { 
    @Override 
    public void onResult(DriveApi.MetadataBufferResult result) { 
     if (!result.getStatus().isSuccess()) { 
     System.out.println("File does not exists"); 
      } else { 
      for(Metadata m : result.getMetadataBuffer()) { 
      if (m.getTitle().equals("locations.csv")) { 
       downloadLink = m.getWebContentLink(); 
       } 
      } 
     } 
     } 
    }); 

在这个文件夹中,只有一个文件,我确信。所以我只需要一种方法将这个文件下载到SD卡或更好的特定文件夹中。

欢迎任何帮助!谢谢。

我的解决方案

在点击代码中的某处

driveFileMy.open(mGoogleApiClient, DriveFile.MODE_READ_ONLY, null) 
        .setResultCallback(contentsOpenedCallback); 

那么我这样做:

final private ResultCallback<DriveApi.DriveContentsResult> contentsOpenedCallback = 
     new ResultCallback<DriveApi.DriveContentsResult>() { 
      @Override 
      public void onResult(DriveApi.DriveContentsResult result) { 
       if (!result.getStatus().isSuccess()) { 
        // display an error saying file can't be opened 
        return; 
       } 
       // DriveContents object contains pointers 
       // to the actual byte stream 
       DriveContents contents = result.getDriveContents(); 
       InputStream inputStream = contents.getInputStream(); 
       OutputStream out = null; 
       try { 
        out = new FileOutputStream(filelocation); 
        byte[] buffer = new byte[1024]; 
        int read; 
        while ((read = inputStream.read(buffer)) != -1) { 
         out.write(buffer, 0, read); 
        } 

        out.flush(); 
        inputStream.close(); 
        out.close(); 

       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     }; 

字符串filelocation是输出文件,它应该的位置保存。感谢大家 :)!

+0

如果您的downloadLink有一个链接,您可以通过浏览器下载链接,它不仅打开与InputStream的链接并将内容保存在SD卡中? – jonathanrz

回答

1

Drive API具有full guide on working with file contents,即获得文件的直接InputStream

给出一个DriveFile,你

DriveFile file = ... 
file.open(mGoogleApiClient, DriveFile.MODE_READ_ONLY, null) 
    .setResultCallback(contentsOpenedCallback); 

打开它使用ResultCallback,看起来像:

ResultCallback<DriveContentsResult> contentsOpenedCallback = 
    new ResultCallback<DriveContentsResult>() { 
    @Override 
    public void onResult(DriveContentsResult result) { 
    if (!result.getStatus().isSuccess()) { 
     // display an error saying file can't be opened 
     return; 
    } 
    // DriveContents object contains pointers 
    // to the actual byte stream 
    DriveContents contents = result.getDriveContents(); 
    } 
}; 

您可以从InputStream通过contents.getInputStream()然后读,写出来给本地文件,如果你想。

请记住,自谷歌驱动已经保持一个本地副本一旦你打开一个文件,如果你只需要你的应用程序中的内容,最好是使用InputStream,而不是直接将其保存到尚未另一个本地文件(加倍所需的存储空间)。

+0

我的不好,我怎么会错过这个部分...从来没有与InputStream合作过,但我会试图找出它。我需要特定文件夹中的本地副本,以便我的应用可以访问它。谢谢,我会发布一个代码,如果我会得到它的工作。 – Wladislaw

+0

您的应用程序已经在访问每个字节对应的'InputStream',但是没关系。祝你好运! – ianhanniballake

相关问题