Search code examples
javaandroidfacebook-graph-apifacebook-android-sdkfacebook-sdk-4.0

Failed to send image from one activity to another. Please see details


I'm fetching user's profile picture from facebook and I want to send it to ProfileActivity.java so that it can be displayed on user profile.

The problem is that the image is not getting sent from SignUpScreen.java to ProfileActivity.java. Though I am able to send name & email from one to another.

Here's SignUpScreen.java file's code:

public class SignUpScreen extends AppCompatActivity  {

    Button facebookLoginButton;
    CircleImageView mProfileImage;
    TextView mUsername, mEmailID;
    Profile mFbProfile;
    ParseUser user;
    Bitmap bmp = null;
    public String name, email, userID;
    public static final List<String> mPermissions = new ArrayList<String>() {{
        add("public_profile");
        add("email");
    }};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_sign_up_screen);

        TextView textView = (TextView) findViewById(R.id.h);
        Typeface typeface = Typeface.createFromAsset(getBaseContext().getAssets(), "fonts/Pac.ttf");
        textView.setTypeface(typeface);

        mProfileImage = (CircleImageView) findViewById(R.id.user_profile_image);
        mUsername = (TextView) findViewById(R.id.userName);
        mEmailID = (TextView) findViewById(R.id.aboutUser);

        mFbProfile = Profile.getCurrentProfile();

        //mUsername.setVisibility(View.INVISIBLE);
        //mEmailID.setVisibility(View.INVISIBLE);

        facebookLoginButton = (Button) findViewById(R.id.facebook_login_button);
        facebookLoginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                ParseFacebookUtils.logInWithReadPermissionsInBackground(SignUpScreen.this, mPermissions, new LogInCallback() {
                    @Override
                    public void done(ParseUser user, ParseException err) {

                        if (user == null) {
                            Log.d("MyApp", "Uh oh. The user cancelled the Facebook login.");
                        } else if (user.isNew()) {
                            Log.d("MyApp", "User signed up and logged in through Facebook!");
                            getUserDetailsFromFacebook();
                            final Handler handler3 = new Handler();
                            handler3.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    saveNewUser();
                                }
                            }, 5000);
                        } else {
                            Log.d("MyApp", "User logged in through Facebook!");
                        }
                    }
                });

            }
        });

    }

    public void saveNewUser() {
        user = new ParseUser();
        user.setUsername(name);
        user.setEmail(email);
        user.setPassword("hidden");

        user.signUpInBackground(new SignUpCallback() {
            @Override
            public void done(ParseException e) {
                if (e == null) {
                    Toast.makeText(SignUpScreen.this, "SignUp Succesful", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(SignUpScreen.this, "SignUp Unsuccesful", Toast.LENGTH_LONG).show();
                    Log.d("error when signingup", e.toString());
                }
            }
        });
    }

    private void getUserDetailsFromFacebook() {

        final GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
                new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(
                            JSONObject object,
                            GraphResponse response) {
                        // Application code
                        //Log.d("response", "response" + object.toString());
                        Intent profileIntent = new Intent(SignUpScreen.this, ProfileActivity.class);
                        Bundle b = new Bundle();
                        try {
                            name = response.getJSONObject().getString("name");
                            mUsername.setText(name);
                            email = response.getJSONObject().getString("email");
                            mEmailID.setText(email);
                            userID = response.getJSONObject().getString("id");
                            new ProfilePicAsync().execute(userID);

                            b.putString("userName", name);
                            b.putString("userEmail", email);

                            profileIntent.putExtras(b);
                            profileIntent.putExtra("user_pic", bmp);
                            startActivity(profileIntent);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });

        Bundle parameters = new Bundle();
        parameters.putString("fields", "name, email, id");
        request.setParameters(parameters);
        request.executeAsync();

    }

    class ProfilePicAsync extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... params) {

            String imageURL;
            String id = userID;
            imageURL = "https://graph.facebook.com/"+ id +"/picture?type=large";
            try {
                bmp = BitmapFactory.decodeStream((InputStream)new URL(imageURL).getContent());
            } catch (Exception e) {
                e.printStackTrace();
                Log.d("Loading picture failed", e.toString());

            }

            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            mProfileImage.setImageBitmap(bmp);
        }

    }
}

Here's ProfileActivity.java file's code:

public class ProfileActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        Bundle bundle = getIntent().getExtras();

        CircleImageView mProfileImage = (CircleImageView) findViewById(R.id.user_profile_image);
        TextView mUsername = (TextView) findViewById(R.id.userName);
        TextView mEmailID = (TextView) findViewById(R.id.aboutUser);

        Bitmap bitmap = (Bitmap) getIntent().getParcelableExtra("user_pic");
        mProfileImage.setImageBitmap(bitmap);

        mUsername.setText(bundle.getString("userName"));
        mEmailID.setText(bundle.getString("userEmail"));

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

}

Please let me know what is going wrong here.


Solution

  • In your getUserDetailsFromFacebook() method you have called new ProfilePicAsync().execute(userID) to get the image. But it seems that before you could fetch the image ,startActivity(profileIntent) probably gets called. First be sure that you have fetched the image from facebook before you call startActivity(profileIntent).

    EDIT

    Add this to your getUserDetailsFromFacebook() ,

    b.putString("userName", name);
    b.putString("userEmail", email);
    profileIntent.putExtras(b);
    
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();
    profileIntent.putExtra("user_pic", byteArray);
    
    startActivity(profileIntent);
    

    Add this to your ProfileActivity.java ,

    byte[] byteArray = getIntent().getByteArrayExtra("user_pic");
    Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    mProfileImage.setImageBitmap(bmp);