2015-05-24 85 views
2

我可以從谷歌驅動器獲取文件的驅動器ID。 通過以下代碼。如何獲取谷歌驅動器的文件的URL在Android下載

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

現在,我想該文件的URL,這樣我可以通過URL使用此LINK

+0

您是否嘗試過使用file.getDownloadUrl()? 「 –

+0

」getDownloadUrl()「沒有這樣的方法@ BidhanA – kkkk

+0

你能給我一個你得到的driveId的例子嗎?打印driveId.getResourceId()時會得到什麼? –

回答

0

下面的代碼將獲得url供您下載。

String downloadUrl = file.getWebContentLink();

用一個簡單的谷歌搜索發現: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; 
     } 
    } 

    // ... 
} 
+0

它告訴投到'文件'。我應該施放它? @SuperThomasLab – kkkk

+0

@kkkk什麼行?我真的不知道,我剛剛從谷歌獲得這些信息。只要嘗試,如果它的工作,然後... –

+0

它通過崩潰給出了一個錯誤。 – kkkk

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

我這個錯誤:java.lang.RuntimeException:失敗的結果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:伺機不得在UI線程調用@Bidhan一個 – kkkk

+0

哎呀。我忘了。 await()方法將阻塞線程直到獲得結果。它不應該在UI線程中調用。您應該將此方法放在回調方法或某個後臺線程中。你知道怎麼做嗎? –

+0

我不明白,因爲我是新的。 – kkkk

0

您正在使用錯誤的庫。

您目前正在使用Google Drive Android API。這僅允許訪問存儲在設備上的雲端硬盤應用程序中的文件。它不允許您訪問雲中的雲端硬盤文件。這就是爲什麼你看到classCastExceptions。

首先刪除所有導入語句,並將它們替換爲Google Drive Java庫中的等價物。作爲一個簡單的線索,任何含有.gms的導入都是不正確的。

0

如果主要目的是下載文件,可以在android-sdk中使用示例。 您將需要driveID,但如果你有這個,可以使用該示例。您將在這裏找到它: ./android-sdk/extras/google/google_play_services/drive/demo 查找該文件名爲:RetreiveContentsWithProgressDialog.java

的文件顯示你如何寫一個進度下載。

相關問題