我正在研究谷歌驱动器并尝试在我的 app.lots 中融入我所做的很多事情。首先我创建 SH1 密钥,然后启用驱动器 api,然后生成 APP 密钥。现在我已经实现了这个链接,但是当我从图库中选择图像时,它重定向回这个活动,我打印图像的路径,它给出了正确的路径和不上传。我现在做什么?请任何人帮助我,我在这里击中了我现在无法理解的内容。
https://developers.google.com/drive/quickstart-android#step_4_set_up_the_sample
import java.io.IOException;
import android.accounts.AccountManager;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
import com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException;
import com.google.api.client.http.FileContent;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File;
public class MainActivity extends Activity{
private static final int REQUEST_ACCOUNT_PICKER = 1;
private static final int SELECT_PICTURE = 3;
private static final int REQUEST_AUTHORIZATION = 2;
private static Drive service;
private GoogleAccountCredential credential;
private static Uri selectedImageUri;
private String selectedImagePath;
private ImageView img;
private TextView tv;
Intent pictureintent;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// img = (ImageView)findViewById(R.id.ImageView01);
((Button) findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
pictureintent = new Intent();
pictureintent.setType("image/jpeg");
pictureintent.setAction(Intent.ACTION_GET_CONTENT);
pictureintent.putExtra(MediaStore.EXTRA_OUTPUT, selectedImageUri);
startActivityForResult(pictureintent, SELECT_PICTURE);
}
});
}
public void onActivityResult(int requestCode, int resultCode, 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) {
credential.setSelectedAccountName(accountName);
service = getDriveService(credential);
saveFileToDrive();
}
}
break;
case REQUEST_AUTHORIZATION:
if (resultCode == Activity.RESULT_OK) {
saveFileToDrive();
} else {
startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
}
case SELECT_PICTURE:
if (resultCode == Activity.RESULT_OK) {
selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
// tv = (TextView) findViewById(R.id.text1);
//tv.setText("File Path: " + selectedImagePath);
showToast("File Path: " + selectedImagePath);
System.out.println("Image Path : " + selectedImagePath);
//img.setImageURI(selectedImageUri);
startpictureIntent();
}
}
}
private Drive getDriveService(GoogleAccountCredential credential) {
return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential)
.build();
}
public void startpictureIntent(){
credential = GoogleAccountCredential.usingOAuth2(this, DriveScopes.DRIVE);
startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
}
private void saveFileToDrive() {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
// File's binary content
java.io.File fileContent = new java.io.File(selectedImageUri.getPath());
FileContent mediaContent = new FileContent("image/jpeg", fileContent);
// File's metadata.
File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType("image/jpeg");
File file = service.files().insert(body, mediaContent).execute();
if(file != null) {
showToast("Photo uploaded: " + file.getTitle());
}
}catch (UserRecoverableAuthIOException e) {
startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
} catch (IOException e) {
e.printStackTrace();
}
}
});
t.start();
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImageUri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
public void showToast(final String toast) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_LONG).show();
}
});
}
}