0

I have managed to move both the Name and Email to the next activity but when I try to move the Image, It crashes....I have stored the name and Email in variables and also managed to get the path of the Image. How can I be able to move the Image to the next Activity...?

  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>

Log cat

    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) 

2 Answers2

1

If you successfully get user-id, Make a query to get the profile picture directly from your second activity.

 https://www.googleapis.com/plus/v1/people/{GOOGLE_USER_ID}?key={YOUR_API_KEY}

where:

GOOGLE_USER_ID - ID of the user in Google Plus

YOUR_API_KEY - Android API key

Shivam Kamat
  • 121
  • 12
0

Save the image locally if you are able to access it from first activity (like mentioned in the comment) and then load the locally saved image file in the next activity.

I think the path is inaccessible after the life of the GoogleSignInOptions is over. And in your second activity it definitely is over.

Use this to save the image locally on 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

Community
  • 1
  • 1
Viral Patel
  • 32,418
  • 18
  • 82
  • 110