質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Q&A

0回答

334閲覧

androidでgoogle drive APIを使ってドライブ上ファイルを取得したい

退会済みユーザー

退会済みユーザー

総合スコア0

Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

0グッド

1クリップ

投稿2018/06/27 02:47

Android* アプリケーションから Google Drive* に接続する

上記URLを参考に、androidでgoogle drive APIを使ってドライブ上ファイルを取得しようとしています。
以下のコード(MainActivity.java)をDLし実行しました。

Java

1package com.intel.driveapp; 2 3import ... 4 5public class MainActivity extends Activity 6{ 7 static final int REQUEST_ACCOUNT_PICKER = 1; 8 static final int REQUEST_AUTHORIZATION = 2; 9 static final int RESULT_STORE_FILE = 4; 10 private static Uri mFileUri; 11 private static Drive mService; 12 private GoogleAccountCredential mCredential; 13 private Context mContext; 14 private List<File> mResultList; 15 private ListView mListView; 16 private String[] mFileArray; 17 private String mDLVal; 18 private ArrayAdapter mAdapter; 19 20 @Override 21 protected void onCreate(Bundle savedInstanceState) 22 { 23 super.onCreate(savedInstanceState); 24 25 // Connect to Google Drive 26 mCredential = GoogleAccountCredential.usingOAuth2(this, Arrays.asList(DriveScopes.DRIVE)); 27 startActivityForResult(mCredential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); 28 29 mContext = getApplicationContext(); 30 31 setContentView(R.layout.activity_main); 32 mListView = (ListView) findViewById(R.id.listView1); 33 34 OnItemClickListener mMessageClickedHandler = new OnItemClickListener() 35 { 36 public void onItemClick(AdapterView parent, View v, int position, long id) 37 { 38 downloadItemFromList(position); 39 } 40 }; 41 42 mListView.setOnItemClickListener(mMessageClickedHandler); 43 44 final Button button = (Button) findViewById(R.id.button1); 45 button.setOnClickListener(new View.OnClickListener() 46 { 47 public void onClick(View v) 48 { 49 final Intent galleryIntent = new Intent(Intent.ACTION_PICK); 50 galleryIntent.setType("*/*"); 51 startActivityForResult(galleryIntent, RESULT_STORE_FILE); 52 } 53 }); 54 55 final Button button2 = (Button) findViewById(R.id.button2); 56 button2.setOnClickListener(new View.OnClickListener() 57 { 58 public void onClick(View v) 59 { 60 getDriveContents(); 61 } 62 }); 63 } 64 65 private void getDriveContents() 66 { 67 Thread t = new Thread(new Runnable() 68 { 69 @Override 70 public void run() 71 { 72 mResultList = new ArrayList<File>(); 73 com.google.api.services.drive.Drive.Files f1 = mService.files(); 74 com.google.api.services.drive.Drive.Files.List request = null; 75 76 do 77 { 78 try 79 { 80 request = f1.list(); 81 request.setQ("trashed=false"); 82 com.google.api.services.drive.model.FileList fileList = request.execute(); 83 84 mResultList.addAll(fileList.getItems()); 85 request.setPageToken(fileList.getNextPageToken()); 86 } catch (UserRecoverableAuthIOException e) { 87 startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION); 88 } catch (IOException e) { 89 e.printStackTrace(); 90 if (request != null) 91 { 92 request.setPageToken(null); 93 } 94 } 95 } while (request.getPageToken() !=null && request.getPageToken().length() > 0); 96 97 populateListView(); 98 } 99 }); 100 t.start(); 101 } 102 103 private void downloadItemFromList(int position) 104 { 105 mDLVal = (String) mListView.getItemAtPosition(position); 106 showToast("You just pressed: " + mDLVal); 107 108 Thread t = new Thread(new Runnable() 109 { 110 @Override 111 public void run() 112 { 113 for(File tmp : mResultList) 114 { 115 if (tmp.getTitle().equalsIgnoreCase(mDLVal)) 116 { 117 if (tmp.getDownloadUrl() != null && tmp.getDownloadUrl().length() >0) 118 { 119 try 120 { 121 com.google.api.client.http.HttpResponse resp = 122 mService.getRequestFactory() 123 .buildGetRequest(new GenericUrl(tmp.getDownloadUrl())) 124 .execute(); 125 InputStream iStream = resp.getContent(); 126 try 127 { 128 final java.io.File file = new java.io.File(Environment 129 .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath(), 130 tmp.getTitle()); 131 showToast("Downloading: " + tmp.getTitle() + " to " + Environment 132 .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath()); 133 storeFile(file, iStream); 134 } finally { 135 iStream.close(); 136 } 137 138 } catch (IOException e) { 139 e.printStackTrace(); 140 } 141 } 142 } 143 } 144 } 145 }); 146 t.start(); 147 } 148 149 private void populateListView() 150 { 151 runOnUiThread(new Runnable() 152 { 153 @Override 154 public void run() 155 { 156 mFileArray = new String[mResultList.size()]; 157 int i = 0; 158 for(File tmp : mResultList) 159 { 160 mFileArray[i] = tmp.getTitle(); 161 i++; 162 } 163 mAdapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1, mFileArray); 164 mListView.setAdapter(mAdapter); 165 } 166 }); 167 } 168 169 private void storeFile(java.io.File file, InputStream iStream) 170 { 171 try 172 { 173 final OutputStream oStream = new FileOutputStream(file); 174 try 175 { 176 try 177 { 178 final byte[] buffer = new byte[1024]; 179 int read; 180 while ((read = iStream.read(buffer)) != -1) 181 { 182 oStream.write(buffer, 0, read); 183 } 184 oStream.flush(); 185 } finally { 186 oStream.close(); 187 } 188 } catch (Exception e) { 189 e.printStackTrace(); 190 } 191 } catch (IOException e) { 192 e.printStackTrace(); 193 } 194 } 195 196 @Override 197 public boolean onCreateOptionsMenu(Menu menu) { 198 getMenuInflater().inflate(R.menu.main, menu); 199 return true; 200 } 201 202 @Override 203 protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) 204 { 205 switch (requestCode) 206 { 207 case REQUEST_ACCOUNT_PICKER: 208 if (resultCode == RESULT_OK && data != null && data.getExtras() != null) 209 { 210 String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME); 211 if (accountName != null) { 212 mCredential.setSelectedAccountName(accountName); 213 mService = getDriveService(mCredential); 214 } 215 } 216 break; 217 case REQUEST_AUTHORIZATION: 218 if (resultCode == Activity.RESULT_OK) { 219 //account already picked 220 } else { 221 startActivityForResult(mCredential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); 222 } 223 break; 224 case RESULT_STORE_FILE: 225 mFileUri = data.getData(); 226 // Save the file to Google Drive 227 saveFileToDrive(); 228 break; 229 } 230 } 231 232 private Drive getDriveService(GoogleAccountCredential credential) { 233 return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential) 234 .build(); 235 } 236 237 238 private void saveFileToDrive() 239 { 240 Thread t = new Thread(new Runnable() 241 { 242 @Override 243 public void run() 244 { 245 try 246 { 247 // Create URI from real path 248 String path; 249 path = getPathFromUri(mFileUri); 250 mFileUri = Uri.fromFile(new java.io.File(path)); 251 252 ContentResolver cR = MainActivity.this.getContentResolver(); 253 254 // File's binary content 255 java.io.File fileContent = new java.io.File(mFileUri.getPath()); 256 FileContent mediaContent = new FileContent(cR.getType(mFileUri), fileContent); 257 258 showToast("Selected " + mFileUri.getPath() + "to upload"); 259 260 // File's meta data. 261 File body = new File(); 262 body.setTitle(fileContent.getName()); 263 body.setMimeType(cR.getType(mFileUri)); 264 265 com.google.api.services.drive.Drive.Files f1 = mService.files(); 266 com.google.api.services.drive.Drive.Files.Insert i1 = f1.insert(body, mediaContent); 267 File file = i1.execute(); 268 269 if (file != null) 270 { 271 showToast("Uploaded: " + file.getTitle()); 272 } 273 } catch (UserRecoverableAuthIOException e) { 274 startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION); 275 } catch (IOException e) { 276 e.printStackTrace(); 277 showToast("Transfer ERROR: " + e.toString()); 278 } 279 } 280 }); 281 t.start(); 282 } 283 284 public void showToast(final String toast) { 285 runOnUiThread(new Runnable() { 286 @Override 287 public void run() { 288 Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show(); 289 } 290 }); 291 } 292 293 public String getPathFromUri(Uri uri) 294 { 295 String[] projection = { MediaStore.Images.Media.DATA }; 296 Cursor cursor = getContentResolver().query(uri, projection, null, null, null); 297 int column_index = cursor 298 .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 299 cursor.moveToFirst(); 300 return cursor.getString(column_index); 301 } 302 303} 304

実行したところ、MainActivity.javaのgetDriveContents()メソッドの中のtry節内のcom.google.api.services.drive.model.FileList fileList = request.execute();で以下のエラーが出てしまいます。

java.lang.IllegalArgumentException: the name must not be empty: null

何かの変数が正常にセットされていないために、null落ちしてしまうのでしょうか?

ご回答お願いします

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問