从Google云端硬盘获取列表文件与Java [英] Get list file from GoogleDrive with Java

查看:167
本文介绍了从Google云端硬盘获取列表文件与Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个应用程序,可以登录,下载,谷歌从驱动器上传和显示列表文件。

I'm developing an app that can login, download, upload and show list file from Google Drive.

我发现的Java code从的谷歌驱动器SDK 但它运行很慢,不工作。

I've found Java code from Google Drive SDK but it run quite slow and isn't work.

我在读解决方案,但不知道在哪里更改范围

I'm reading solution but don't know where to change that scope

我mainActivity:

My mainActivity:

        // google
        static final int REQUEST_ACCOUNT_PICKER = 1;
        static final int REQUEST_AUTHORIZATION = 2;
        static final int CAPTURE_IMAGE = 3;
        private GoogleAccountCredential credential;
        private static Uri fileUri;
        private static Drive service;

        public void onLoginGoogle(View v) {
                credential = GoogleAccountCredential.usingOAuth2(this, DriveScopes.DRIVE);
                startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
        }

        public void onUploadGoogle(View v) {
                Thread t = new Thread(new Runnable() {
                        @Override
                        public void run() {
                                try {
                                        // File's binary content
                                        java.io.File fileContent = new java.io.File("/sdcard/a.jpg");
                                        FileContent mediaContent = new FileContent("image/jpeg", fileContent);

                                        // File's metadata.
                                        com.google.api.services.drive.model.File body = new com.google.api.services.drive.model.File();
                                        body.setTitle(fileContent.getName());
                                        body.setMimeType("image/jpeg");

                                        com.google.api.services.drive.model.File file = service.files().insert(body, mediaContent).execute();
                                        if (file != null) {
                                                showToast("Photo uploaded: " + file.getTitle());
                                                // startCameraIntent();
                                        }
                                } catch (UserRecoverableAuthIOException e) {
                                        startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
                                } catch (IOException e) {
                                        e.printStackTrace();
                                }
                        }
                });
                t.start();
        }

        public void onDownloadGoogle(View v) {
                downloadFile();
        }

        private void downloadFile() {
                Thread thread = new Thread(new Runnable() {

                        @Override
                        public void run() {
                                try {
                                        com.google.api.services.drive.model.File file = service.files().get("0B-Iak7O9SfIpYk9zTjZvY2xreVU").execute();
                                        // FileList file = service.files().list().execute();
                                        // List<com.google.api.services.drive.model.File> fileList =
                                        // file.getItems();
                                        // com.google.api.services.drive.model.File fileItem =
                                        // fileList.get(0);
                                        // Log.d("FileID" , fileItem.getId());
                                        // Log.d("Count", "Retreived file list");
                                        if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0) {
                                                HttpResponse resp = service.getRequestFactory().buildGetRequest(new GenericUrl(file.getDownloadUrl())).execute();
                                                InputStream inputStream = resp.getContent();
                                                // writeToFile(inputStream);
                                        }
                                } catch (IOException e) {
                                        Log.e("WriteToFile", e.toString());
                                        e.printStackTrace();
                                }
                        }
                });
                thread.start();
        }

        List<File> mGFile;

        public void onListGoogle(View v) {
                AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {

                        @Override
                        protected String doInBackground(Void... arg0) {
                                // TODO Auto-generated method stub
                                try {
                                        mGFile = retrieveAllFiles();
                                        int i = 0;
                                } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }
                                return null;
                        }

                        protected void onPostExecute(String result) {
                                Log.d("Dolphin get glist", String.valueOf(mGFile.size()));
                        };
                };
                task.execute();
                Log.d("Dolphin counting", "aa");
        }

        private List<File> retrieveAllFiles() throws IOException {
                List<File> result = new ArrayList<File>();
                Files.List request = service.files().list();

                do {
                        try {
                                FileList files = request.execute();

                                result.addAll(files.getItems());
                                request.setPageToken(files.getNextPageToken());
                        } catch (IOException e) {
                                System.out.println("An error occurred: " + e);
                                request.setPageToken(null);
                        }
                } while (request.getPageToken() != null && request.getPageToken().length() > 0);

                return result;
        }

        private Drive getDriveService(GoogleAccountCredential credential) {
                return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential).build();
        }

@Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {

                switch (requestCode) {
                case REQUEST_ACCOUNT_PICKER:
                        if (resultCode == RESULT_OK && data != null && data.getExtras() != null) {
                                String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
                                if (accountName != null) {
                                        credential.setSelectedAccountName(accountName);
                                        service = getDriveService(credential);
                                }
                        }
                        break;
                case REQUEST_AUTHORIZATION:
                        if (resultCode == Activity.RESULT_OK) {
                                // saveFileToDrive();
                                Toast.makeText(this, "Login complete", Toast.LENGTH_SHORT);
                        } else {
                                startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
                        }
                        break;
                }
        }

在logcat的:

The logcat:

"Application name is not set. Call Builder#setApplicationName."

如何获得文件列表从文件夹?这是真棒,如果有一个工作的例子

How to get list of files from a folder? It's awesome if have a working example

推荐答案

这为我的作品:

private List<File> sendRequest(){
    List<File> result = new ArrayList<File>();

    try{
        Files.List request = service.files().list().setQ("trashed = false");

        do{
            try{
                FileList files = request.execute();

                result.addAll(files.getItems());
                request.setPageToken(files.getNextPageToken());
            }
            catch(IOException e){
                System.out.println("An error occurred: " + e);
                request.setPageToken(null);
            }
        }
        while(request.getPageToken() != null
                && request.getPageToken().length() > 0);

    }
    catch(IOException e1){
        e1.printStackTrace();
    }

    for(File file : result){
        Log.d(TAG, "file : " + file.getTitle());
    }
    return result;
}

如果你想从一个特定的文件夹中的文件,你可以试试这个要求:

If you want files from a specific folder, you can try this request:

Files.List request = service.files().list().setQ("'" + folderId + "' in parents and trashed = false");

正如你所说,这是pretty慢,但我不知道该怎么做的更好。

As you said, it's pretty slow, but I don't know how to do better.

这篇关于从Google云端硬盘获取列表文件与Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆