2

I can get the drive id of a file from the google drive. by following code.

import com.example.googledrivetest2.ProcessDownload.DownloadFileAsync;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.Result;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.drive.Drive;
import com.google.android.gms.drive.DriveApi;
import com.google.android.gms.drive.DriveApi.DriveContentsResult;
import com.google.android.gms.drive.DriveApi.DriveIdResult;
import com.google.android.gms.drive.DriveContents;
import com.google.android.gms.drive.DriveFile;
import com.google.android.gms.drive.DriveFolder;
import com.google.android.gms.drive.DriveFolder.DriveFileResult;
import com.google.android.gms.drive.DriveId;
import com.google.android.gms.drive.DriveResource;
import com.google.android.gms.drive.Metadata;
import com.google.android.gms.drive.MetadataChangeSet;
import com.google.android.gms.drive.OpenFileActivityBuilder;

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.i(TAG, "in onActivityResult() - triggered on pressing Select");

        switch (requestCode) {

            case REQUEST_CODE_SELECT:
                if (resultCode == RESULT_OK) {
                    /*get the selected item's ID*/
                    DriveId driveId = (DriveId) data.getParcelableExtra(
                            OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);//this extra contains the drive id of the selected file
                    Log.i(TAG, "Selected folder's ID: " + driveId.encodeToString());
                    Log.i(TAG, "Selected folder's Resource ID: " + driveId.getResourceId());// this is the id of the actual file
                    Toast.makeText(getApplicationContext()," my id: "+driveId.getResourceId() , Toast.LENGTH_LONG).show();

                    DriveFile file = Drive.DriveApi.getFile(googleApiClient,driveId);
                   ....
                     }
                };

Now i want to get the URL of that file so that i can pass the URL to download using this LINK

kkkk
  • 121
  • 1
  • 1
  • 9

4 Answers4

2

Have you tried this method yet?

DriveFile file = Drive.DriveApi.getFile(googleApiClient,driveId);
MetadataResult mdRslt = file.getMetadata(googleApiClient).await();
if (mdRslt != null && mdRslt.getStatus().isSuccess()) {
   String link = mdRslt.getMetadata().getWebContentLink();
   Log.d("LINK", link);
}
Bidhan
  • 10,607
  • 3
  • 39
  • 50
  • I got this error:java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=102, result=-1, data=Intent { act=android.intent.action.PICK (has extras) }} to activity {com.example.googledrivetest2/com.example.googledrivetest2.DownloadActivity}: java.lang.IllegalStateException: await must not be called on the UI thread @Bidhan A – kkkk May 24 '15 at 11:36
  • Oops. I forgot. await() method is going to block the thread until it gets the result. It should not be called in the UI thread. You should put this method inside of a callback method or some background thread. Do you know how to do that? – Bidhan May 24 '15 at 11:41
  • I didn't understand as i am new . – kkkk May 24 '15 at 11:47
  • Could you tell me how to do that?? @Bidhan A – kkkk May 25 '15 at 03:05
0

The code below will get the url for you.

String downloadUrl = file.getWebContentLink();

Found it with a simple Google search: https://developers.google.com/drive/web/manage-downloads#downloading_files_in_a_browser

Full code:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.i(TAG, "in onActivityResult() - triggered on pressing Select");

    switch (requestCode) {

        case REQUEST_CODE_SELECT:
            if (resultCode == RESULT_OK) {
                /*get the selected item's ID*/
                DriveId driveId = (DriveId) data.getParcelableExtra(
                        OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);//this extra contains the drive id of the selected file
                Log.i(TAG, "Selected folder's ID: " + driveId.encodeToString());
                Log.i(TAG, "Selected folder's Resource ID: " + driveId.getResourceId());// this is the id of the actual file
                Toast.makeText(getApplicationContext(), " my id: " + driveId.getResourceId(), Toast.LENGTH_LONG).show();

                DriveFile file = Drive.DriveApi.getFile(googleApiClient, driveId);

                //get download url
                String downloadUrl = file.getWebContentLink();
                //do something with the url, for example:
                System.out.println("Download URL: " + downloadUrl);
                //more info here: https://developers.google.com/drive/web/manage-downloads#downloading_files_in_a_browser
            }
    }
}

Example:

import com.google.api.services.drive.Drive;
import com.google.api.services.drive.model.File;

import java.io.IOException;
import java.io.InputStream;

// ...

public class MyClass {

    // ...

    /**
     * Download a file's content.
     *
     * @param service Drive API service instance.
     * @param file Drive File instance.
     * @return InputStream containing the file's content if successful,
     *         {@code null} otherwise.
     */
    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;
        }
    }

    // ...
}
Thomas Vos
  • 12,271
  • 5
  • 33
  • 71
  • It tells to cast to 'file' . Should i cast it?? @SuperThomasLab – kkkk May 24 '15 at 10:21
  • @kkkk what line? and I honestly don't know, I just got this information from google. Just try if it work then... – Thomas Vos May 24 '15 at 10:25
  • java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=102, result=-1, data=Intent { act=android.intent.action.PICK (has extras) }} to activity {com.example.googledrivetest2/com.example.googledrivetest2.DownloadActivity}: java.lang.ClassCastException: com.google.android.gms.drive.internal.zzu cannot be cast to com.google.android.gms.drive.Metadata 05-24 16:32:14.669: E/AndroidRuntime(5885): at android.app.ActivityThread.deliverResults(ActivityThread.java:3525) – kkkk May 24 '15 at 10:33
  • It's a cast exception. Have you copied the full code i gave you? – Thomas Vos May 24 '15 at 10:35
  • i copied esp these lines DriveFile file = Drive.DriveApi.getFile(googleApiClient, driveId); String downloadUrl = file.getWebContentLink(); System.out.println("Download URL: " + downloadUrl); – kkkk May 24 '15 at 10:39
  • have you understood the fact yet?? @SuperThomasLab – kkkk May 24 '15 at 10:49
  • @kkkk what line does it crash? – Thomas Vos May 24 '15 at 10:52
  • @kkkk And will this question help you, it also has a progressbar: http://stackoverflow.com/questions/20834703/download-a-file-from-google-drive-by-url-in-android – Thomas Vos May 24 '15 at 10:53
0

You're using the wrong library.

You are currently using the Google Drive Android API. This ONLY gives access to files stored in the Drive app on your device. It does NOT give you access to your Drive files in the cloud. This is why you are seeing classCastExceptions.

Start by deleting all of your import statements, and replace them with their equivalents from the Google Drive Java library. As a simple clue, any import with .gms is incorrect.

pinoyyid
  • 21,499
  • 14
  • 64
  • 115
0

If the main purpose is to download a file, you can use an example in your android-sdk. You will need the driveID, but if you got that, the example can be used. You will find it here: ./android-sdk/extras/google/google_play_services/drive/demo Look for the file named: RetreiveContentsWithProgressDialog.java

The file shows you how to write a download with a progressbar.

Skywalker
  • 1,717
  • 1
  • 22
  • 25