Google drive call authorizattion request - android

I was able to upload a picture to my Google Drive with this example based on the Google Drive SDK. My problem now is that the authorizattion request (the activity where is displayed which permissions the app has) is called indirectly by this code:
private static Drive service;
try
{
// ...
File file = service.files().insert(body, mediaContent).execute();
//...
}
catch (UserRecoverableAuthIOException e)
{
startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
}
I want to show this activity without creating an exception. Can I call this activity from a method of the service object?

There is no way to start this activity without handling UserRecoverableAuthIOException or UserRecoverableAuthException.

Related

Google Drive API - download quota exceeded

I'm getting an error:
The download quota for this file has been exceeded
This is a response from the drive api. I'm trying to download a file simply via following url:
String url = "https://www.googleapis.com/drive/v2/files/" + media.getId() + "?alt=media\n"
My file is a few MBs big and I just can't download it. I'm using scribe and oauth2, I create a request, sign it and send it. The response shows me that the signature works, but I don't know why I always get the above error response from google...
Other things like retrieving a list of all my files and my user work just fine and work multiple times as well...
Since you're using Android, why not try to call it using the Java API client, as discussed in the Download Files section of the documentation.
private static InputStream downloadFile(Drive service, File file) {
if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0) {
try {
// uses alt=media query parameter to request content
return service.files().get(file.getId()).executeMediaAsInputStream();
} catch (IOException e) {
// An error occurred.
e.printStackTrace();
return null;
}
} else {
// The file doesn't have any content stored on Drive.
return null;
}
}
Another alternative is to use the webContentLink to retrieve the download URL.
Hope this helps!
Log in multcloud
Add Cloud (Google Drive) & Login and Allow Drive
select the file and Share get public url

Use new File() with Android Studio

I am getting a File Not Found exception when I try to access any files using Android Studio. I am able to open a text file with AssetManager but I need to open a p12 file for oAuth Authentication. The code I'm using is taken from https://code.google.com/p/google-api-java-client/wiki/OAuth2#Service_accounts
It seems that files can't be accessed this way in Android Studio? What is the alternative? I am trying to display events from a public calendar so I'm not even really sure I need oAuth (I didn't for a web app).
GoogleCredential credential = null;
try {
credential = new GoogleCredential.Builder().setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes(Collections.singleton(PlusScopes.PLUS_ME))
.setServiceAccountPrivateKeyFromP12File(new File(KEY))
.build();
} catch (GeneralSecurityException e) {
e.printStackTrace();
} catch (IOException e) {
Log.d("FUCK", "cant open");
e.printStackTrace();
}
new File(KEY) creates a file object pointing to the path KEY. That file doesn't exist, so you have problems. If your p12 file is in assets, you'll need to copy it to your phone's file system first and pass in the file, rather than the asset name.

How to delete file from Google Drive

I am using android-demos for implementing Google Drive integration in android. I successfully created a file in google drive. Now i want to delete that newly created file. I found the reference for this through https://developers.google.com/drive/v2/reference/files/delete Now files() method in this link is not found in Drive.
private static void deleteFile(Drive service, String fileId) {
try {
service.files().delete(fileId).execute();
} catch (IOException e) {
System.out.println("An error occurred: " + e);
}
}
Now please tell me how to delete file from Google Drive. I did research on this but found no solution. Some says use previous api of Google Drive. But that is obsolete now. Now Goole uses V2 for Drive.
Last time I checked, there is no delete in GDAA. See How to delete a file on google drive using Google Drive Android API
You can either wait for it to be implemented, or use the REST API https://developers.google.com/drive/v2/reference/files/delete
I suspect you are confusing two different APIs. GDAA is a purely local API, ie. your app is communicating with the Android Drive app. With the REST API, your app is talking over http to the Google Drive servers. Your app can use either, or a mixture of both (although you need to be pretty desparate to do that).
Deleting files from Google drive using core API is not yet supported. So you must use Restful API calls. To do restful API calls you need to add following jars to your lib folder
google-api-client-1.19.1.jar
google-api-client-android-1.19.1.jar
google-api-services-drive-v2-rev158-1.19.1.jar
google-http-client-1.19.0.jar
google-http-client-android-1.19.0.jar
google-http-client-gson-1.19.0.jar
google-oauth-client-1.19.0.jar
gson-2.1.jar
jsr305-1.3.9.jar
now you can do restful API call withing core API calls as follows
com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential crd = GoogleAccountCredential
.usingOAuth2(
ctx,
Arrays.asList(com.google.api.services.drive.DriveScopes.DRIVE_FILE));
crd.setSelectedAccountName(email);
_drvSvc = new com.google.api.services.drive.Drive.Builder(
AndroidHttp.newCompatibleTransport(), new GsonFactory(), crd).setApplicationName("SmsAndCallLogBackup")
.build();
remember i connected to Google drive using core API. and for deleting only I am using restful API
following method is used to delete file from Google drive
public void delete(DriveId dId) {
try {
String fileID = dId.getResourceId();
if (fileID != null)
_drvSvc.files().delete(fileID).execute();
} catch (Exception e) {
e.printStackTrace();
}
}
call this method in async task otherwise it gives error
it will work definitely
I have done it successfull
**
private GoogleApiClient api;
**
public void Update(DriveId dId) {
try {
DriveFile sumFile = dId.asDriveFile();
com.google.android.gms.common.api.Status deleteStatus =
sumFile.delete(api).await();
if (!deleteStatus.isSuccess()) {
Log.e(TAG, "Unable to delete app data.");
} else {
// Remove stored DriveId.
preferences_driverId.edit().remove("drive_id").apply();
}
} catch (Exception e) {
e.printStackTrace();
}
}

Delete data of one application from other in android

Can we clear or delete data of one application from another application in android. If it is not possible simply could you please suggest any tricky way to do this like to go to the folder in internal memory and delete that folder programatically.
Thanks.
Simple answer,
Android is designed that this should not be possible.
But when using root access you can actually delete folders from other applications.
I believe if two applications having a different package, but with the same signature, actually can have access to each others private folders. Or i'm not sure, i believe you could add some kind of declaration to you manifest file allowing other (friend) apps to have access to your private folder. But i'm not sure i should search for it.
Edit after search:
Apps having the same android:sharedUserId and android:sharedUserLabel and signature have access to each others private files.
http://developer.android.com/guide/topics/manifest/manifest-element.html#uid
Two Android applications with the same user ID
Edit 2:
There are some private methods in the android API, witch can be used to clear app data i think. I'm not sure but if you reflect those methods with the right permissions in you manifest file it could be possible to clear app data, but i'm not 100% sure.
Some small example code:
Method clearApplicationUserData = getReflectedMethod("clearApplicationUserData", String.class, IPackageDataObserver.class);
And the method i use the get it reflected...
private Method getReflectedMethod(String methodname, Class<?>... args) {
Method temp = null;
try {
temp = pm.getClass().getMethod(methodname, args);
} catch (SecurityException e) {
return null;
} catch (NoSuchMethodException e) {
return null;
}
return temp;
}
The IPackageDataObserver class should be copied from the original android source, and added as new class in the source folder of your project under the package android.content.pm.
When you want to clear user data i think you should invoke the method like this:
public void clearApplicationUserData(String packageName) {
if (clearApplicationUserData != null) {
try {
clearApplicationUserData.invoke(pm, packageName, data_helper);
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
}
}
The data_helper is any class extending the IPackageDataObserver.Stub class.
You can find a lot of questions about reflecting methods and stuff here on stackoverflow.
I have no idea if this works but this is the only way i can think of.
Rolf
If its stored in the database you can delete the data using content provider by following method
mRowsDeleted = getContentResolver().delete(
UserDictionary.Words.CONTENT_URI, // the user dictionary content URI
mSelectionClause // the column to select on
mSelectionArgs // the value to compare to
);
follow the methods Here
The question is not very clear.
Do you mean your app will delete data from someone elses app? The answer there is "it depends". First, if the data is on the SD card, you could access it and delete it. If the data is in the apps private data area, then you cannot do that unless your phone is rooted.
If the apps in question are both made by you, the answer is yes, it is possible. You would have to use the android:sharedUserId property in the manifest file of each app, make them the same and sign both apps with the same key. This would give the apps access to each others data.
If the data you are speaking of is on the devices SD card, yes you can. If it is in the internal storage, then no (unless you created both apps and have used the

How to Load the KMZ file?

"I have to load a kmz file in to the android application that i am developing and that kmz file will be loaded from sdcard in to the application. So what i should do for that whether there is direct uri intent or i have to parse it by xml parsing if so then how to load coordinates in to the map to show that kmz file.
To use the native v3 KmlLayer, which supports kmz files, subject to the documented size and complexity restrictions, the kmz file must be publicly available on the web (so google's servers can get to it).
To use a local file (which it sounds like you do), your only option is to use a third party parser like geoxml3 or geoxml-v3.
to open in android studio use below code :
try {
KmlLayer layer = new KmlLayer(mGoogleMap,R.raw.filename,
getApplicationContext());
// creating the kml layer, put the file in res/raw
layer.addLayerToMap();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

Categories

Resources