使用Java从GoogleDrive获取列表文件

使用Java从GoogleDrive获取列表文件,第1张

概述我正在开发一个可以登录,下载,上传和显示Google云端硬盘列表文件的应用.我发现Java代码来自GoogleDriveSDK,但运行速度很慢,无法正常工作.我正在阅读solution,但不知道在哪里更改该范围我的mainActivity://googlestaticfinalintREQUEST_ACCOUNT_PICKER

我正在开发一个可以登录,下载,上传和显示Google云端硬盘列表文件的应用.

我发现Java代码来自Google Drive SDK,但运行速度很慢,无法正常工作.

我正在阅读solution,但不知道在哪里更改该范围

我的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;                }        }

日志:

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

如何从文件夹中获取文件列表?如果有一个可行的例子真棒

解决方法:

这个对我有用:

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;}

如果您想要来自特定文件夹的文件,则可以尝试以下请求:

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

如您所说,它的速度很慢,但是我不知道如何做得更好.

总结

以上是内存溢出为你收集整理的使用Java从GoogleDrive获取列表文件全部内容,希望文章能够帮你解决使用Java从GoogleDrive获取列表文件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1093175.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存