我正在使用Android应用程序与谷歌驱动器集成。我想上传.csv文件的备份到谷歌驱动器..但发现错误。我的代码在这里......从库中选择图片后出现错误:com.google.api.client.googleapis.extentions.android.gms.auth.GoogleAuthIOException
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Connect to Google Drive
mCredential = GoogleAccountCredential.usingAudience(MainActivity.this,"852032254538-54h2nh1h5f2c4bg7ubash64d4vtcsc4f.apps.googleusercontent.com");
startActivityForResult(mCredential.newChooseAccountIntent(),
REQUEST_ACCOUNT_PICKER);
mContext = MainActivity.this;
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.listView1);
OnItemClickListener mMessageClickedHandler = new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,
long id) {
downloadItemFromList(position);
}
};
mListView.setOnItemClickListener(mMessageClickedHandler);
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final Intent galleryIntent = new Intent(Intent.ACTION_PICK);
galleryIntent.setType("*/*");
startActivityForResult(galleryIntent, RESULT_STORE_FILE);
}
});
final Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
mResultList = new ArrayList<File>();
com.google.api.services.drive.Drive.Files f1 = mService
.files();
Files.List request = null;
do {
try {
request = f1.list();
request.setQ("trashed=false");
FileList fileList = request.execute();
mResultList.addAll(fileList.getItems());
request.setPageToken(fileList
.getNextPageToken());
} catch (UserRecoverableAuthIOException e) {
startActivityForResult(e.getIntent(),
REQUEST_AUTHORIZATION);
} catch (IOException e) {
e.printStackTrace();
if (request != null) {
request.setPageToken(null);
}
}
} while (request.getPageToken() != null
&& request.getPageToken().length() > 0);
populateListView();
}
});
t.start();
}
});
}
private void downloadItemFromList(int position) {
mDLVal = (String) mListView.getItemAtPosition(position);
showToast("You just pressed: " + mDLVal);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
for (File tmp : mResultList) {
if (tmp.getTitle().equalsIgnoreCase(mDLVal)) {
if (tmp.getDownloadUrl() != null
&& tmp.getDownloadUrl().length() > 0) {
try {
com.google.api.client.http.HttpResponse resp = mService
.getRequestFactory()
.buildGetRequest(
new GenericUrl(tmp
.getDownloadUrl()))
.execute();
InputStream iStream = resp.getContent();
try {
final java.io.File file = new java.io.File(
Environment
.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS)
.getPath(), tmp.getTitle());
showToast("Downloading: "
+ tmp.getTitle()
+ " to "
+ Environment
.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS)
.getPath());
storeFile(file, iStream);
} finally {
iStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
});
t.start();
}
private void populateListView() {
runOnUiThread(new Runnable() {
@Override
public void run() {
mFileArray = new String[mResultList.size()];
int i = 0;
for (File tmp : mResultList) {
mFileArray[i] = tmp.getTitle();
i++;
}
mAdapter = new ArrayAdapter<String>(mContext,
android.R.layout.simple_list_item_1, mFileArray);
mListView.setAdapter(mAdapter);
}
});
}
private void storeFile(java.io.File file, InputStream iStream) {
try {
final OutputStream oStream = new FileOutputStream(file);
try {
try {
final byte[] buffer = new byte[1024];
int read;
while ((read = iStream.read(buffer)) != -1) {
oStream.write(buffer, 0, read);
}
oStream.flush();
} finally {
oStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected void onActivityResult(final int requestCode,
final int resultCode, final Intent data) {
switch (requestCode) {
case REQUEST_ACCOUNT_PICKER:
if (resultCode == RESULT_OK && data != null
&& data.getExtras() != null) {
String accountName = data
.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
if (accountName != null) {
mCredential.setSelectedAccountName(accountName);
mService = getDriveService(mCredential);
}
}
break;
case REQUEST_AUTHORIZATION:
if (resultCode == Activity.RESULT_OK) {
// account already picked
} else {
startActivityForResult(mCredential.newChooseAccountIntent(),
REQUEST_ACCOUNT_PICKER);
}
break;
case RESULT_STORE_FILE:
mFileUri = data.getData();
// Save the file to Google Drive
saveFileToDrive();
break;
}
}
private Drive getDriveService(GoogleAccountCredential credential) {
return new Drive.Builder(AndroidHttp.newCompatibleTransport(),
new GsonFactory(), credential).build();
}
private void saveFileToDrive() {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
// Create URI from real path
String path;
path = getPathFromUri(mFileUri);
mFileUri = Uri.fromFile(new java.io.File(path));
ContentResolver cR = MainActivity.this.getContentResolver();
Log.d("tag123", "go to save in drive");
// File's binary content
java.io.File fileContent = new java.io.File(mFileUri
.getPath());
Log.d("tag123", "get path from resourse");
FileContent mediaContent = new FileContent(cR
.getType(mFileUri), fileContent);
Log.d("tag123", "selected");
showToast("Selected " + mFileUri.getPath() + "to upload");
// File's meta data.
File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType(cR.getType(mFileUri));
Log.d("tag123", "store in other file");
com.google.api.services.drive.model.File file = mService
.files().insert(body, mediaContent).execute();
if (file != null) {
showToast("Uploaded: " + file.getTitle());
}
} catch (UserRecoverableAuthIOException e) {
startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
} catch (IOException e) {
e.printStackTrace();
showToast("Transfer ERROR: " + e.toString());
}
}
});
t.start();
}
public void showToast(final String toast) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), toast,
Toast.LENGTH_SHORT).show();
}
});
}
public String getPathFromUri(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null,
null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}