Skip to content Skip to sidebar Skip to footer

How To Get The Url Of A File Of Google Drive To Download In Android

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.comm

Solution 1:

Have you tried this method yet?

DriveFilefile= Drive.DriveApi.getFile(googleApiClient,driveId);
MetadataResultmdRslt= file.getMetadata(googleApiClient).await();
if (mdRslt != null && mdRslt.getStatus().isSuccess()) {
   Stringlink= mdRslt.getMetadata().getWebContentLink();
   Log.d("LINK", link);
}

Solution 2:

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:

@OverrideprotectedvoidonActivityResult(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*/DriveIddriveId= (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();

                DriveFilefile= Drive.DriveApi.getFile(googleApiClient, driveId);

                //get download urlStringdownloadUrl= 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;

// ...publicclassMyClass {

    // .../**
     * 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.
     */privatestatic InputStream downloadFile(Drive service, File file) {
        if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0) {
            try {
                // uses alt=media query parameter to request contentreturn service.files().get(file.getId()).executeMediaAsInputStream();
            } catch (IOException e) {
                // An error occurred.
                e.printStackTrace();
                returnnull;
            }
        } else {
            // The file doesn't have any content stored on Drive.returnnull;
        }
    }

    // ...
}

Solution 3:

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.

Solution 4:

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.

Post a Comment for "How To Get The Url Of A File Of Google Drive To Download In Android"