从Google drive Android获取文件列表

从Google drive Android获取文件列表,android,google-drive-android-api,Android,Google Drive Android Api,我试图在我的android项目中使用google Drive,我成功地从该驱动器下载了一个文件,但没有成功地从google Drive获得文件列表,我已经工作了2周,但我找不到解决方案 public class RetrieveContentsActivity extends Activity implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener { /*get

我试图在我的android项目中使用google Drive,我成功地从该驱动器下载了一个文件,但没有成功地从google Drive获得文件列表,我已经工作了2周,但我找不到解决方案

   public class RetrieveContentsActivity extends Activity implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener {

/*get this id from your google drive on the web*/
private static final String EXISTING_FILE_ID = "0B1_qyX0EdGokaWVVSWxzd2hLdWc";
private static final int REQUEST_CODE = 102;
private GoogleApiClient googleApiClient;
private static final String TAG = "retrieve_contents";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_retrieve_contents);
    /*build the api client*/
    buildGoogleApiClient();

}

@Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    Log.i(TAG, "In onStart() - connecting...");
    googleApiClient.connect();

}

@Override
protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
    if (googleApiClient != null) {
        Log.i(TAG, "In onStop() - disConnecting...");
        googleApiClient.disconnect();
        }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
        Log.i(TAG, "In onActivityResult() - connecting...");
        googleApiClient.connect();
        }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.retrieve_contents, menu);
    return true;
}



@Override
public void onConnected(Bundle bundle) {
    // TODO Auto-generated method stub
    Drive.DriveApi.fetchDriveId(googleApiClient, EXISTING_FILE_ID).setResultCallback(idCallback);

        }

@Override
public void onConnectionSuspended(int arg0) {
    // TODO Auto-generated method stub
    Drive.DriveApi.fetchDriveId(googleApiClient, EXISTING_FILE_ID).setResultCallback(idCallback);

}

/*callback on getting the drive id, contained in result*/
final private ResultCallback<DriveIdResult> idCallback = new ResultCallback<DriveIdResult>() {
@Override
public void onResult(DriveIdResult result) {
    DriveFile file = Drive.DriveApi.getFile(googleApiClient, result.getDriveId());
    /*use a pending result to get the file contents */
    PendingResult<ContentsResult> pendingResult = file.openContents(googleApiClient, DriveFile.MODE_READ_ONLY, null);

    /*the callback receives the contents in the result*/
    pendingResult.setResultCallback(new  ResultCallback<ContentsResult>() {
    public String fileAsString;
    @Override
    public void onResult(ContentsResult result) {
    Contents fileContents = result.getContents();
    InputStream iStream = fileContents.getInputStream();

    Log.i(TAG, "reading input stream and building string...");

    //Create folder To Save the File Downloaded
    File root = new File(Environment.getExternalStorageDirectory(),"Téléchargement");
    if (!root.exists()) {root.mkdirs();}
    //Create File 
    File file = new File(root, "lena.jpg");
    storeFile(file, iStream);

    //fileContents.discard(googleApiClient);
    Intent intent = new Intent(RetrieveContentsActivity.this, DisplayFileActivity.class);
    intent.putExtra("text", fileAsString);
    startActivity(intent);
    }
    });
    }
    };
    /*callback when there there's an error connecting the client to the service.*/
    @Override
    public void onConnectionFailed(ConnectionResult result) {
    Log.i(TAG, "Connection failed");
    if (!result.hasResolution()) {
    GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this, 0).show();
    return;
    }
    try {
        result.startResolutionForResult(this, REQUEST_CODE);
    } catch (IntentSender.SendIntentException e) {
    Log.e(TAG, "Exception while starting resolution activity", e);
    }
    }
    /*build the google api client*/
    private void buildGoogleApiClient() {
    if (googleApiClient == null) {
    googleApiClient = new GoogleApiClient.Builder(this)
    .addApi(Drive.API)
    .addScope(Drive.SCOPE_FILE)
    .addConnectionCallbacks(this)
    .addOnConnectionFailedListener(this)
    .build();
    }

}
    private void storeFile(File file, InputStream iStream)
    {
        try 
        {
            final OutputStream oStream = new FileOutputStream(file);
            try
            {
                try
                {
                    final byte[] buffer = new byte[1024];
                    int read;
                    while ((read = iStream.read(buffer)) != -1)
                    {
                        oStream.write(buffer, 0, read);
                    }
                    oStream.flush();
                } finally {
                    oStream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

假设您已经尝试了运气和演示,您也可以尝试我编写的一段代码。它有5个基本的SCRUD搜索、创建、读取、更新和删除函数,用于和API。如果您设法使它在您的环境中工作,它应该为您提供所有基本的构建块,包括您需要的身份验证和帐户切换

请记住,由于GDAA只支持文件范围,所以您不会看到Android应用程序未创建的文件。您必须使用带驱动器作用域的REST api来搜索由其他应用程序(如web DRIVE)创建的文件/文件夹


祝你好运

,是的,我已经尝试了所有这些教程,但都不起作用,我需要在我的代码中添加一个方法,以从Google drive获取文件列表。如果你不介意,你能帮我吗?你想列出驻留在Google drive上的所有文件/文件夹吗,即使是由其他应用程序(如web interface)创建的文件,还是仅由Android应用程序创建的文件?是的,所有驻留在Google Drive上的文件,即使是由其他应用程序创建的文件。在本例中,答案的第二段中,您不能使用GDAA api。AFAIK的唯一选项是1/Go to Main活动,并将所有GDAA前缀重命名为REST前缀。2/修改“testTree”方法,使其不会解压缩JPEG文件。我假设-从您的其他信息中找不到com.google.api.services.drive.driveBuilder类,您缺少JAR依赖项。请查看RESTAPI所需的依赖项。