In BaseDemoActivity.java from Google Drive Demo app
https://github.com/googledrive/android-demos/blob/master/src/com/google/android/gms/drive/sample/demo/BaseDemoActivity.java
what i have replace in EXISTING_FOLDER_ID,EXISTING_FILE_ID,EXTRA_ACCOUNT_NAME
Please provide steps to get this values
/**
* DriveId of an existing folder to be used as a parent folder in
* folder operations samples.
*/
public static final String EXISTING_FOLDER_ID = "????";
/**
* DriveId of an existing file to be used in file operation samples..
*/
public static final String EXISTING_FILE_ID = "????";
/**
* Extra for account name.
*/
protected static final String EXTRA_ACCOUNT_NAME = "????";
Thanks
The first part, EXISTING_FOLDER_ID,EXISTING_FILE_ID, is answered in SO 21800257, but be careful.
You can't just go to a web Drive interface, create a file, copy/paste its resource id ... Google Drive Android API (GDAA) supports only FILE scope, so only files, folders created by your Android App are eligible.
EXTRA_ACCOUNT_NAME is a name of your Google (gmail) account (myaccount#gmail.com). That's the one you would need here:
GoogleApiClient mGac = new GoogleApiClient.Builder(contex)
.addApi(com.google.android.gms.drive.Drive.API)
.addScope(com.google.android.gms.drive.Drive.SCOPE_FILE)
.setAccountName(EXTRA_ACCOUNT_NAME)
.addConnectionCallbacks(context).addOnConnectionFailedListener(context)
.build();
but I'm not sure the demo even addresses this.
You would use it if your Android App allowed switching between different accounts.
Related
I'd like for my app to be able to read from a pre-defined shared public Google Drive folder without the user having to log in or choose a Google account.
Background / Environment
Using my desktop browser, I have created a public folder on my Google Drive that is set up to be public. Anyone with the link may access (read) the drive, so no authorization is required:
In my Android Studio project, I have gone into File > Project Structure > Dependencies and added com.google.android.gms:play-services-drive:10.2.0
I now have the ability to create a new GoogleApiClient.Builder().
Question
I have looked at various examples, but in most cases, the drive has been created by the Android application. This is not the situation I'm trying to manage.
This question is about accessing a drive that has been made public using the "folder ID" or whatever you call 0B6X74x23H.... that was assigned when the folder was originally shared and made public.
I have examined the demo code provided by Google, but that, presumably, is not for a public folder because it says:
...need to register an OAuth 2.0 client
At a minimum, I could drive the process by using http-client, going to the sharing link https://drive.google.com/drive/folders/0B6X74x23Hx7DNE13M0ZIbVI....?usp=sharing with no authentication and not need to jump through hoops. But of course, it would be cleaner to use a defined API and simply specify the public shared folder in order to list the contents and, if needed, download the files from the public folder.
When I try this code:
Scope publicFolder = new Scope(EXISTING_FOLDER_ID);
mGoogleApiClient = new GoogleApiClient.Builder(mActivity)
.addApi(Drive.API)
.addScope(publicFolder)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
This method fires:
GoogleApiClient.OnConnectionFailedListener.onConnectionFailed()
The result contains statusCode=SIGN_IN_REQUIRED. But of course, sign-in is NOT required for a folder that's public.
Here is a workaround with Jsoup
implementation 'org.jsoup:jsoup:1.11.3'
val url = "https://drive.google.com/drive/folders/xxxxxxxxxxxxxxxxxx" // shared folder link
val doc = Jsoup.connect(url).get()
doc.outputSettings().prettyPrint(false)
val files = doc.select("div.WYuW0e")
for (file in files){
val fileName = file.text()
val fileID = file.attr("data-id")
val downloadLink = "https://drive.google.com/uc?export=download&id=$fileID"
//the downloadLink may open a 'Google Drive can't scan this file for viruses' page
// below we check for the new link
val doc2 = Jsoup.connect(downloadLink).get()
doc2.outputSettings().prettyPrint(false)
val elem = doc2.select("[id='uc-download-link']")
val newLink = if (elem.size != 0){
"https://drive.google.com" + elem.first().attr("href")
} else {
downloadLink
}
}
I want to retrieve metadata from files or folders in Google Drive on Android device in order to get deviceID or resourceID, so then I could download the file to local storage of the device. The problem with my application is that, the application doesn't need user interaction. It mean that, just tell the application the name so then the app will try to find the files in every directory and download it.
I have try to use query (link) follow quickstart tutorial but it return me only the name of the existing file only.
Note again: User does not need to select files or folders, just tell the name of the file is enough. And the file or folder is created by the application too.
How to get metadata from files or folders in Google Drive on Android?
According to Working with File and Folder Metadata:
Metadata is encapsulated in the Metadata class and contains all details about a file or folder including the title, the MIME type, and whether the file is editable, starred or trashed. The metadata is fetched for a DriveResource by calling the DriveResource.getMetadata method.
Here's a snippet from the docs:
/**
* An activity to retrieve the metadata of a file.
*/
public class RetrieveMetadataActivity extends BaseDemoActivity implements
ResultCallback {
#Override
public void onConnected(Bundle connectionHint) {
DriveFile file = Drive.DriveApi.getFile(getGoogleApiClient(),
DriveId.decodeFromString("0ByfSjdPVs9MZcEE3bzJCc3NsRkE"));
file.getMetadata(getGoogleApiClient()).setResultCallback(metadataRetrievedCallback);
}
ResultCallback<MetadataResult> metadataRetrievedCallback = new
ResultCallback<MetadataResult>() {
#Override
public void onResult(MetadataResult result) {
if (!result.getStatus().isSuccess()) {
showMessage("Problem while trying to fetch metadata");
return;
}
Metadata metadata = result.getMetadata();
showMessage("Metadata succesfully fetched. Title: " + metadata.getTitle());
}
}
}
How to get resource ID?
Still on the Android API for Drive, DriveId class has a method getResourceId () which returns the resource ID.
How to get device ID?
I don't think you can use the Android Drive API to get this. It seems you've mistaken this for fileID which you'll be using to Download Files.
Locate fileID manually in Google Drive:
If it's a spreadSheet file
https://docs.google.com/spreadsheets/d/1pE9ejBTBH38oCoOHU2O42qU6vzxagAJ9J1237dYB1Eg/edit#gid=0
fileID -> 1pE9ejBTBH38oCoOHU2O42qU6vzxagAJ9J1237dYB1Eg
If it's a doc file:
https://docs.google.com/document/d/1Fh6s7an-7I6VuDBxZKcxcaU3cG1XpSryHQXGnznWlns/edit
fileID -> 1Fh6s7an-7I6VuDBxZKcxcaU3cG1XpSryHQXGnznWlns
You get the pattern. It's a string of alphanumeric characters in the URL.
I am using google cloud storage to store images on android..I created project in google developers console and gave all id's from that project.
private static final String PROJECT_ID_PROPERTY = "xxxxxxxxxxxxxx"; //project ID
private static final String APPLICATION_NAME_PROPERTY = "refined"; //application name, can be any
private static final String ACCOUNT_ID_PROPERTY = "xxxxxxxxxxxxxxx-n5kqcd842faki0se7s82vpcf9l1rbvui#developer.gserviceaccount.com"; //user account email
Downloaded P12 file and accessing from the code for setting p12 key.
When I tried on 2 days back , images got uploaded correctly and I could see them in the browser..But when I try today the same code, it is giving forbidded issue.
{"code":403,"errors": [{"domain":"global","message":"Forbidden","reason":"forbidden"}],"message":"Forbidden"}
What could be the reason?
I could solve the issue...Issue was with permissions...
I gave write permissions on the bucket for the client ID, then it is working now.
Thanks for all
Let's say I use the Google Drive API to select a file that I want to open. I have this code to do so:
// Getting the drive ID for the file
DriveId driveId = (DriveId) data
.getParcelableExtra(OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);
// Getting the selected file
DriveFile googleDriveFile = Drive.DriveApi.getFile(googleApiClient,
driveId);
googleDriveFile.open(googleApiClient, DriveFile.MODE_READ_ONLY,
null).setResultCallback(
new ResultCallback<DriveContentsResult>() {
#Override
public void onResult(DriveContentsResult result) {
if (result.getStatus().isSuccess()) {
Is there any way for me to get the file name of this file? (Not the drive ID, but its actual name?)
I'm trying to validate the chosen file (by checking its type, which is in the name), and I can't think of a way to do this other than get the name. The type is .cblite, a Couchbase Lite database file. Normally I'd just filter the Drive picker by MIME type, but .cblite (to my knowledge) isn't one of those options. Instead I'm trying to validate by file name.
Is there a way to do this? (Or is there a way to filter MIME types by miscellaneous/unidentified types?)
Yes, once you have DriveId, you can get metadata. And one of the metadata fields is getTitle() which will get you the file / folder name. Look at this code snippet:
DriveFile googleDriveFile = Drive.DriveApi.getFile(googleApiClient, driveId);
MetadataResult mdRslt = googleDriveFile .getMetadata(googleApiClient).await();
if (mdRslt != null && mdRslt.getStatus().isSuccess()) {
mdRslt.getMetadata().getTitle();
}
The 'await' flavor here is used for convenience, it must be run off-ui thread. Or just turn it into a callback style.
Good Luck
public static Intelligence systemLogic;
/**
* #param args
*/
public String getNumber(String filepath) throws Exception{
String number1 = null;
Intelligence grabNumber = null;
grabNumber = new Intelligence(false);
number1 = grabNumber.recognize(new CarSnapshot(filepath));
}
It is failing while create instance itself
EDIT: See the answer on another similar question.
I assume the error happens while you're trying to initialize Intelligence object:
grabNumber = new Intelligence(false);
It happens because the Intelligence object in it's constructor is looking for ./config.xml and cannot find it in your project.
One possible solution would be to find the config.xml file in the JavaANPR jar file and put it in your classpath (if you're using eclipse, your project's main directory).
Another solution would be to create your own project with the JavaANPR source code, and edit the loading of config.xml as I had done. I will opensource my version of JavaANPR shortly.