Missing file when getting files on Google Drive - android

I'm trying to get files on Android from Google Drive but some files are missing. I'm using this code (which can be found here: https://developers.google.com/drive/v2/reference/files/list):
ArrayList<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);
With this code, I get 4 files. However, I have more than 4 files on Google Drive and this 4 files match those that I sent from my application. When I test the "Try it!" section (https://developers.google.com/drive/v2/reference/files/list#try-it), it works fine and all the files are found.
Do you have any ideas on what could be a problem?

Related

ftp4j - Too many connections exception

I am creating a sync service which will copy files to ftp folder selected by user. When i run the service, it connects with ftp and check whether file at android local storage is available at ftp or not and if not, it uploads. Below is my code.
FTPClient ftpClient = new FTPClient();
ftpClient.setPassive(true);
if (!ftpClient.isConnected()) {
ftpClient.connect(server, port);
}else {
Log.i(TAG,"FTP already connected");
}
if (!ftpClient.isAuthenticated()) {
ftpClient.login(username, password);
}else {
Log.i(TAG,"FTP already Logged In");
}
ftpClient.changeDirectory(rfolderpath);
String[] files = ftpClient.listNames();
if (Arrays.asList(files).contains(filename)) {
Log.i(TAG, filename + " Already Found in FTP, Skipping");
} else {
Log.i(TAG, "Sending Go Ahead For Upload");
File file = new File(filepath);
Log.i(TAG, "Uploading File: " + filename);
ftpClient.upload(file);
ftpClient.logout();
ftpClient.disconnect(true);
}
The code works fine for first 8 files and then i start getting exception of too many connections (8) from this IP and my sync terminates.
Here is Error text:
it.sauronsoftware.ftp4j.FTPException [code=421, message= Too many connections (8) from this IP]
Can someone help me how to handle this scenario.
I changed the logic of uploading files. Now created one ftp connection under AsyncTaskLoader and with that upload all files w/o error.

How can I query all the folder and file meta data using Google Drive API Services in one time query?

How can I query metadata of the folders and files using Google Drive API Services, in the one-time for all?
I am using this API com.google.apis:google-api-services-drive:v2-rev170-1.20.0
for my app.
compile('com.google.apis:google-api-services-drive:v2-rev170-1.20.0') {
exclude group: 'org.apache.httpcomponents'
}
public List<File> listFilesInApplicationDataFolder() throws Exception {
List<File> result = new ArrayList<File>();
Drive.Files.List request = null;
try {
request = mService.files().list();
} catch (IOException e) {
e.printStackTrace();
throw new IOException(e);
}
request.setQ("'appfolder' in parents");
do {
try {
FileList files = request.execute();
result.addAll(files.getItems());
request.setPageToken(files.getNextPageToken());
} catch (Exception e) {
System.out.println("An error occurred: " + e);
request.setPageToken(null);
throw new Exception(e);
}
} while (request.getPageToken() != null &&
request.getPageToken().length() > 0);
return result;
}
This method gets all the folder and files from the root folder. But how can I get all the files in the sub-folder, in the one-time query?
My Drive folder structure looks like this:
appdata
--P_Folder_1
--S_Folder_1
--File_1
--File_2
--File_3
--S_Folder_2
--File_1
--File_2
--File_3
--S_Folder_3
--File_1
--File_2
--File_3
--P_Folder_2
--S_Folder_1
--File_1
--File_2
--File_3
--S_Folder_2
--File_1
--File_2
--File_3
--S_Folder_3
--File_1
--File_2
--File_3
Please give suggestions or doc to reference. Thank you so much!!
I think this SO question can give you idea on how to achieve your problem.
You can call the DriveFolder.listChildren that can retrieve a collection of metadata for the direct children of the folder. The result will also include metadata for both files and folders. Here is also the complete code example.
For more information just check the documentation of the API and this SO questions.
Google Drive Android API
get metadata of all files and folders in Google Drive
How to get list of all folders and files from google drive in android

how to read public folder from google drive on android

i want to create a comic reader project on android. In my database, i only save path to the chapter directory which was taken from folderID on google drive. When user request to read comic, i want to through google client api to browse folder and get all file inside it.
i've seen guilde on https://developers.google.com/drive/v2/reference/files/list
but i still do not understand how it works, specials parameter Drive, how can i get it?
Thanks for any supports.
private static List<File> retrieveAllFiles(Drive service) 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;
}

Download thumbnail of file from Google Drive

I can download the original file from Google Drive by using the following code :
public static InputStream downloadFile(Drive service, File file)
throws IOException {
if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0) {
try {
HttpResponse resp = service.getRequestFactory()
.buildGetRequest(new GenericUrl(file.getDownloadUrl()))
.execute();
return resp.getContent();
} catch (IOException e) {
// An error occurred.
e.printStackTrace();
return null;
}
} else {
// The file doesn't have any content stored on Drive.
return null;
}
}
But it looks like the connection really low when downloading many files to add into the grid view.
Therefore, I need to list thumb nail instead of original file as it will be better for the connection.
Please help me how?
Use File.getThumbnail method to get the thumbnail or File.getIconLink to get the icon link.

How to read from Drive application data folder?

This is a follow-up of this question. I have written my data file on Drive application folder, now the same app that created these files needs to retrieve them.
This doesn't work (I get the listing of the file, but the request of file download return with a 401 error):
private ArrayList<File> listFilesInApplicationDataFolder(Drive service) throws IOException {
ArrayList<File> result = new ArrayList<File>();
Files.List request = service.files().list();
request.setQ("'appdata' in parents");
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);
for (File file:result){
System.out.println("##### "+file.getOriginalFilename()+ " "+file.toPrettyString());
if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0) {
try {
HttpResponse resp =
service.getRequestFactory().buildGetRequest(new GenericUrl(file.getDownloadUrl()))
.execute();
InputStream in= resp.getContent();
FileOutputStream fos = openFileOutput("pinmemo",
Context.MODE_PRIVATE);
while(in.available()>0)
fos.write(in.read());
fos.close();
} catch (UserRecoverableAuthIOException e) {
startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
return null;
} catch (IOException e) {
// An error occurred.
e.printStackTrace();
return null;
}
} else {
// The file doesn't have any content stored on Drive.
return null;
}
}
return result;
}
What should I do?
EDIT:
I get this error message:
04-17 11:19:30.614: W/System.err(2022): com.google.api.client.http.HttpResponseException: 401 Unauthorized
04-17 11:19:30.614: W/System.err(2022): at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1095)
EDIT: the new Google play games services basically covers my needs (the only problem is that my app isn't a game...), therefore this question is now partially obsolete. Of course, Google play games uses "Cloud save" instead of "drive", but for my needs that's sufficent. However, should I use Google play games for non-gaming app?
Make sure that your access token has permissions for the appdata scope:
https://www.googleapis.com/auth/drive.appdata

Categories

Resources