How to get the url of a file of google drive to download in android

点点圈 提交于 2019-12-11 03:28:40

问题


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


回答1:


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);
}



回答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:

@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;
        }
    }

    // ...
}



回答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.




回答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.



来源:https://stackoverflow.com/questions/30421889/how-to-get-the-url-of-a-file-of-google-drive-to-download-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!