1

Google код выбора накопителя ниже:Как загрузить выбранный файл Google в выбранный файл на Android?

private void initializeGoogleDrive() { 
    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addApi(Drive.API) 
      .addScope(Drive.SCOPE_FILE) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .build(); 

} 

@Override 
public void onConnected(Bundle bundle) { 
    IntentSender intentSender = Drive.DriveApi 
      .newOpenFileActivityBuilder() 
      .setMimeType(new String[]{"text/plain", "text/html"}) 
      .build(mGoogleApiClient); 
    try { 
     startIntentSenderForResult(
       intentSender, REQUEST_CODE_OPENER, null, 0, 0, 0); 
    } catch (IntentSender.SendIntentException e) { 
     Utility.appLog(TAG, "Unable to send intent"); 
    } 
} 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
if (requestCode == REQUEST_CODE_OPENER) { 
    if (resultCode == RESULT_OK) { 
     DriveId driveId = (DriveId) data.getParcelableExtra(OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID); 
     String resourceId = driveId.getResourceId(); 
     String downloadUrl = "https://drive.google.com/open?id=" + resourceId + "&export=download"; 
     Utility.appLog(TAG, "Download Url :: " + downloadUrl); 
    } 
} 
} 

мы можем получить URL загрузки, но, к сожалению, скачать не может быть сделано, потому что произошла ошибка аутентификации, когда я пытаюсь загрузить файл.

Заранее спасибо.

+0

использования asyc задача с HttpURLConnection setrequst с Идент ключа – Madhur

+0

Я попытался, но произошла ошибка аутентификации, вы можете пожалуйста, пришлите мне полный код для загрузки файла с файлом google? – patel

+0

вам нужно передать authtoken, когда вы запрашиваете URL-адрес ... вы хотите пример ?? – Madhur

ответ

1

ли это в AsyncTask

try{ 
     URL url = new URL(URL); 
     HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
     conn.setRequestMethod("GET"); 
     conn.setRequestProperty("Authorization", "OAuth " + GoogleAuthUtil.getToken(mContext, mCredential.getSelectedAccountName(), Constants.SCOPE)); 
     conn.setDoInput(true); 
     // Starts the query 
     conn.connect(); 
     int responseCode = conn.getResponseCode(); 
     //you will recive the file in input stream 
     File sdcard = Environment.getExternalStorageDirectory(); 
      File file = new File(sdcard, "filename.ext"); 

      FileOutputStream fileOutput = new FileOutputStream(file); 
      InputStream inputStream = conn.getInputStream(); 

      byte[] buffer = new byte[1024]; 
      int bufferLength = 0; 

      while ((bufferLength = inputStream.read(buffer)) > 0) { 
       fileOutput.write(buffer, 0, bufferLength); 
      } 
      fileOutput.close(); 
     } 

Я использую зависимости,

compile 'com.google.android.gms:play-services-identity:8.3.0' 
    compile('com.google.api-client:google-api-client-android:1.20.0') { 
     exclude group: 'org.apache.httpcomponents' 
    } 
+1

Пожалуйста, объясните: mCredential.getSelectedAccountName() и mCredential.getSelectedAccountName() и как я могу получить от моего кода выше? – patel

+0

Имя выбранной учетной записи проверьте это http://stackoverflow.com/questions/35098468/how-to-get-oauth-token-from-googleapiclient/35098661#35098661 – Madhur

+0

Я видел вашу ссылку: В этой ссылке используйте GoogleAccountCredential и ExponentialBackOff. любая зависимость используется для этого класса? – patel

 Смежные вопросы

  • Нет связанных вопросов^_^