我设法将名称和电子邮件移动到下一个活动,但是当我尝试移动图像时,它崩溃了....我已经在变量中存储了名称和电子邮件,并设法获得了图片。我怎样才能将图像移动到下一个活动......?
GoogleLogin.java
package net.simplifiedcoding.blaze;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.GoogleApiClient;
public class GoogleLogin extends AppCompatActivity implements View.OnClickListener, GoogleApiClient.OnConnectionFailedListener {
//Signin button
private SignInButton signInButton;
//Signing Options
private GoogleSignInOptions gso;
//google api client
private GoogleApiClient mGoogleApiClient;
//Signin constant to check the activity result
private int RC_SIGN_IN = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_google);
//Initializing google signin option
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
//Initializing signinbutton
signInButton = (SignInButton) findViewById(R.id.sign_in_button);
signInButton.setSize(SignInButton.SIZE_WIDE);
signInButton.setScopes(gso.getScopeArray());
//Initializing google api client
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
//Setting onclick listener to signing button
signInButton.setOnClickListener(this);
}
//This function will option signing intent
private void signIn() {
//Creating an intent
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
//Starting intent for result
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//If signin
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
//Calling a new function to handle signin
handleSignInResult(result);
}
}
//After the signing we are calling this function
private void handleSignInResult(GoogleSignInResult result) {
//If the login succeed
if (result.isSuccess()) {
//Getting google account
GoogleSignInAccount acct = result.getSignInAccount();
Intent results = new Intent(getApplicationContext(), Result.class);
String strName = acct.getDisplayName();
String strEmail = acct.getEmail();
String strUrl = acct.getPhotoUrl().getPath();
results.putExtra("name", strName);
results.putExtra("email", strEmail);
results.putExtra("url", strUrl);
startActivity(results);
} else {
//If login fails
Toast.makeText(this, "Login Failed", Toast.LENGTH_LONG).show();
}
}
@Override
public void onClick(View v) {
if (v == signInButton) {
//Calling signin
signIn();
}
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}
Result.Java
package net.simplifiedcoding.blaze;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
public class Result extends AppCompatActivity {
private NetworkImageView profilePhoto;
//Image Loader
private ImageLoader imageLoader;
TextView txtName;
TextView txtEmail;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
String name = getIntent().getExtras().getString("name");
String email = getIntent().getExtras().getString("email");
String url = getIntent().getExtras().getString("url");
Toast.makeText(getApplicationContext(), url, Toast.LENGTH_LONG).show();
txtName = (TextView) findViewById(R.id.textViewName);
txtName.setText(name);
txtEmail = (TextView) findViewById(R.id.textViewEmail);
txtEmail.setText(email);
profilePhoto = (NetworkImageView) findViewById(R.id.profileImage);
//Initializing image loader
imageLoader = CustomVolleyRequest.getInstance(this.getApplicationContext())
.getImageLoader();
imageLoader.get(url,
ImageLoader.getImageListener(profilePhoto,
R.mipmap.ic_launcher,
R.mipmap.ic_launcher));
//Loading image
profilePhoto.setImageUrl(url, imageLoader);
}
}
activity_google_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
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=".GoogleLogin">
<com.google.android.gms.common.SignInButton
android:id="@+id/sign_in_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
</LinearLayout>
result.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
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="net.simplifiedcoding.blaze.Result">
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/profileImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textViewName"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="email"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textViewEmail"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
记录猫
12-19 14:26:24.301 31821-31849/net.simplifiedcoding.blaze E/GED: Failed to get GED Log Buf, err(0)
12-19 14:26:25.894 31821-31821/net.simplifiedcoding.blaze E/AndroidRuntime: FATAL EXCEPTION: main
Process: net.simplifiedcoding.blaze, PID: 31821
java.lang.RuntimeException: Unable to start activity ComponentInfo{net.simplifiedcoding.blaze/net.simplifiedcoding.blaze.Result}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2521)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2601)
at android.app.ActivityThread.access$800(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference
at com.android.volley.Request.<init>(Request.java:136)
at com.android.volley.toolbox.ImageRequest.<init>(ImageRequest.java:71)
at com.android.volley.toolbox.ImageLoader.get(ImageLoader.java:219)
at com.android.volley.toolbox.ImageLoader.get(ImageLoader.java:171)
at net.simplifiedcoding.blaze.Result.onCreate(Result.java:48)
at android.app.Activity.performCreate(Activity.java:6092)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2468)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2601)
at android.app.ActivityThread.access$800(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
答案 0 :(得分:1)
如果您成功获得用户ID, 进行查询以直接从第二个活动中获取个人资料照片。
https://www.googleapis.com/plus/v1/people/{GOOGLE_USER_ID}?key={YOUR_API_KEY}
其中:
GOOGLE_USER_ID - Google Plus中用户的ID
YOUR_API_KEY - Android API密钥
答案 1 :(得分:0)
如果您能够从第一个活动(如评论中提到的)访问图像,则在本地保存图像,然后在下一个活动中加载本地保存的图像文件。
我认为在GoogleSignInOptions的生命周期结束后,该路径无法访问。在你的第二次活动中肯定结束了。
使用此选项在sd-card上本地保存图像:
URL url = new URL ("file://some/path/anImage.png");
InputStream input = url.openStream();
try {
//The sdcard directory e.g. '/sdcard' can be used directly, or
//more safely abstracted with getExternalStorageDirectory()
File storagePath = Environment.getExternalStorageDirectory();
OutputStream output = new FileOutputStream (storagePath + "/myImage.png");
try {
byte[] buffer = new byte[aReasonableSize];
int bytesRead = 0;
while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
output.write(buffer, 0, bytesRead);
}
} finally {
output.close();
}
} finally {
input.close();
}
Source of the above code posted
Another good reference on how to save the file from url and rad it again - from same post as above