我可以从google驱动器获取文件的驱动器ID。 通过以下代码。
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);
....
}
};
现在我想获取该文件的网址,以便我可以使用此LINK
传递要下载的网址答案 0 :(得分:2)
你尝试过这种方法吗?
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);
}
答案 1 :(得分:0)
以下代码将为您获取网址。
String downloadUrl = file.getWebContentLink();
通过简单的Google搜索找到它:https://developers.google.com/drive/web/manage-downloads#downloading_files_in_a_browser
完整代码:
@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
}
}
}
示例:
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;
}
}
// ...
}
答案 2 :(得分:0)
您使用的是错误的库。
您目前正在使用Google Drive Android API。这只能访问存储在设备上的云端硬盘应用中的文件。它不允许您访问云中的Drive文件。这就是您看到classCastExceptions的原因。
首先删除所有导入语句,然后将其替换为Google Drive Java库中的等效语句。作为一个简单的线索,任何使用.gms的导入都是错误的。
答案 3 :(得分:0)
如果主要目的是下载文件,您可以使用android-sdk中的示例。 您将需要driveID,但如果您有,则可以使用该示例。你会在这里找到它: ./android-sdk/extras/google/google_play_services/drive/demo 查找名为:RetreiveContentsWithProgressDialog.java
的文件该文件显示了如何使用进度条编写下载。