iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 17
1
Mobile Development

Android開發系列 第 17

[Day17]連接Google雲端並上傳檔案_1

大家好今天要來示範如何上傳文件到Google雲端硬碟。
以下是我們要在gradle新增的代碼:

 implementation 'com.google.android.gms:play-services-auth:18.0.0'
 implementation 'com.google.http-client:google-http-client-gson:1.26.0' 
 implementation 'com.google.api-client:google-api-client-android:1.26.0'
 implementation 'com.google.apis:google-api-services-drive:v3-rev136-1.25.0'

新增下面代碼到gradle的android block裡面

packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/ASL2.0'
        exclude("META-INF/*.kotlin_module")
    }
compileOptions {
        sourceCompatibility = 1.8
        targetCompatibility = 1.8
    }

在Manifest裡面新增以下code:
需要用到GET_ACCOUNTS,INTERNET,以及讀寫檔案的權限

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

在Android studio的project先告一段落,現在我們先到下面的網頁進行設定
https://console.developers.google.com/
先新增一個專案

取好名字點選建立

在建立憑證之前先設定OAuth同意畫面,在這我們先勾選外部然後建立。

設定應用程式名稱然後儲存

接下來我們建立憑證

應用程式類型選擇Android
套件名稱就是Android Studio的專案名稱,在AndroiManifest.xml可以看到


也可以在build.gradle裡面看到專案名稱

SHA-1 憑證指紋
點選右邊的Gradle -> app -> Tasks -> android -> signingReport 就能得到SHA-1

記得要把下圖中圈起來的地方改回app,否則會不能執行app

接下來在 Google Developers Console 啟用 Google Drive API
點選 啟用API和服務 -> 搜尋Google Drice Api 點選啟用


這個網站就設定得差不多了,下一天我們開始進入Android Studio開始打程式碼。


上一篇
[Day16]簡單的ListView
下一篇
[Day18] 連接Google雲端並上傳檔案_2
系列文
Android開發30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言