我正在尝试使用以下教程:https://developers.google.com/drive/quickstart-android。在完成所有步骤并运行代码后,我收到以下错误:''我已经尝试过两次做项目,看看我是否错过了任何步骤,但两次都让我得到了相同的结果。以下是我的活动,清单文件和源文件。
我按照说明下载了“Drive API”。我正在使用针对Android 4.1.2版本的Galaxy SIII。但是,我的项目设置为使用“Google API Android 4.2.2”
运行以下是连接到互联网和4G电话服务时获得的错误:
11-18 15:19:51.275: W/AbstractGoogleClient(3450): Application name is not set. Call Builder#setApplicationName.
11-18 15:19:51.946: W/IInputConnectionWrapper(3450): showStatusIcon on inactive InputConnection
11-18 15:20:02.367: E/ActivityThread(3450): Activity com.example.fileupload.MainActivity has leaked ServiceConnection com.google.android.gms.internal.by@41e4e5a8 that was originally bound here
11-18 15:20:02.367: E/ActivityThread(3450): android.app.ServiceConnectionLeaked: Activity com.example.fileupload.MainActivity has leaked ServiceConnection com.google.android.gms.internal.by@41e4e5a8 that was originally bound here
11-18 15:20:02.367: E/ActivityThread(3450): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:965)
11-18 15:20:02.367: E/ActivityThread(3450): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:859)
11-18 15:20:02.367: E/ActivityThread(3450): at android.app.ContextImpl.bindService(ContextImpl.java:1308)
11-18 15:20:02.367: E/ActivityThread(3450): at android.app.ContextImpl.bindService(ContextImpl.java:1300)
11-18 15:20:02.367: E/ActivityThread(3450): at android.content.ContextWrapper.bindService(ContextWrapper.java:401)
11-18 15:20:02.367: E/ActivityThread(3450): at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
11-18 15:20:02.367: E/ActivityThread(3450): at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
11-18 15:20:02.367: E/ActivityThread(3450): at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.getToken(GoogleAccountCredential.java:192)
11-18 15:20:02.367: E/ActivityThread(3450): at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:217)
11-18 15:20:02.367: E/ActivityThread(3450): at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:836)
11-18 15:20:02.367: E/ActivityThread(3450): at com.google.api.client.googleapis.media.MediaHttpUploader.executeCurrentRequest(MediaHttpUploader.java:456)
11-18 15:20:02.367: E/ActivityThread(3450): at com.google.api.client.googleapis.media.MediaHttpUploader.executeCurrentRequestWithBackOffAndGZip(MediaHttpUploader.java:478)
11-18 15:20:02.367: E/ActivityThread(3450): at com.google.api.client.googleapis.media.MediaHttpUploader.executeUploadInitiation(MediaHttpUploader.java:428)
11-18 15:20:02.367: E/ActivityThread(3450): at com.google.api.client.googleapis.media.MediaHttpUploader.upload(MediaHttpUploader.java:326)
11-18 15:20:02.367: E/ActivityThread(3450): at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:420)
11-18 15:20:02.367: E/ActivityThread(3450): at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:345)
11-18 15:20:02.367: E/ActivityThread(3450): at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:463)
11-18 15:20:02.367: E/ActivityThread(3450): at com.example.fileupload.MainActivity$1.run(MainActivity.java:98)
11-18 15:20:02.367: E/ActivityThread(3450): at java.lang.Thread.run(Thread.java:856)
11-18 15:20:02.377: I/Adreno200-EGLSUB(3450): <ConfigWindowMatch:2087>: Format RGBA_8888.
11-18 15:20:06.021: I/Adreno200-EGLSUB(3450): <ConfigWindowMatch:2087>: Format RGBA_8888.
11-18 15:20:16.782: I/Adreno200-EGLSUB(3450): <ConfigWindowMatch:2087>: Format RGBA_8888.
11-18 15:20:27.764: W/AbstractGoogleClient(3450): Application name is not set. Call Builder#setApplicationName.
11-18 15:20:27.844: W/AbstractGoogleClient(3450): Application name is not set. Call Builder#setApplicationName.
11-18 15:20:28.925: W/IInputConnectionWrapper(3450): showStatusIcon on inactive InputConnection
以下是从教程
复制的代码{
package com.example.fileupload;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import android.accounts.AccountManager;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
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;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
static final int REQUEST_ACCOUNT_PICKER = 1;
static final int REQUEST_AUTHORIZATION = 2;
static final int CAPTURE_IMAGE = 3;
private static Uri fileUri;
private static Drive service;
private GoogleAccountCredential credential;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
credential = GoogleAccountCredential.usingOAuth2(this, DriveScopes.DRIVE);
startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
}
@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) {
credential.setSelectedAccountName(accountName);
service = getDriveService(credential);
startCameraIntent();
}
}
break;
case REQUEST_AUTHORIZATION:
if (resultCode == Activity.RESULT_OK) {
saveFileToDrive();
} else {
startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
}
break;
case CAPTURE_IMAGE:
if (resultCode == Activity.RESULT_OK) {
saveFileToDrive();
}
}
}
private void startCameraIntent() {
String mediaStorageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).getPath();
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
fileUri = Uri.fromFile(new java.io.File(mediaStorageDir + java.io.File.separator + "IMG_"
+ timeStamp + ".jpg"));
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(cameraIntent, CAPTURE_IMAGE);
}
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(fileUri.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());
startCameraIntent();
}
} catch (UserRecoverableAuthIOException e) {
startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
} catch (IOException e) {
e.printStackTrace();
}
}
});
t.start();
}
private Drive getDriveService(GoogleAccountCredential credential) {
return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential)
.build();
}
public void showToast(final String toast) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show();
}
});
}
}
}
清单文件 {
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fileupload"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="16" />
<permission
android:name="com.google.cloud.backend.android.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.cloud.backend.android.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.fileupload.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="com.google.android.apps.drive.APP_ID" android:value="*****" />
<intent-filter>
<action android:name="com.google.android.apps.drive.DRIVE_OPEN" />
<data android:mimeType="application/vnd.google-apps.drive-sdk.id=*****" />
<data android:mimeType="image/png" />
<data android:mimeType="image/jpeg" />
<data android:mimeType="image/jpg" />
</intent-filter>
</activity>
</application>
</manifest>
}
布局文件 {
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
}
答案 0 :(得分:0)
在日志中,第一行说明了在Google控制台上为简单的api访问密钥注册应用时添加的应用程序名称。 我在由凭证对象提供时构建了我的Drive对象,如下所示:
public static Drive getDriveService(GoogleAccountCredential credential)
{
return
new Drive
.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential)
.setApplicationName("YourProjectName")
.build();
}
我希望这能解决你的问题!