Shubhendra Shubhendra - 5 days ago 6
Android Question

How to pass the user image(fetched from Facebook profile) from navbar to another activity

This is my Activity 1

final Intent ridesameIntent = new Intent(this, Activity2.class);
TextView userText = (TextView) findViewById(R.id.username);
ImageView userImage = (ImageView) findViewById(R.id.profile_pic);
String userNameMessage = userText.getText().toString();
String userPicMessage = userImage.toString();
ridesameIntent.putExtra(EXTRA_MESSAGE_USERNAME,userNameMessage);
ridesameIntent.putExtra(EXTRA_MESSAGE_USERPIC,userPicMessage);
startActivity(ridesameIntent);


This is Activity 2

Intent userDetail = getIntent();
String userPicMessage = userDetail.getStringExtra(Activity1.EXTRA_MESSAGE_USERPIC);
String userNameMessage = userDetail.getStringExtra(Activity1.EXTRA_MESSAGE_USERNAME);
TextView userdetail1 = (TextView) findViewById(R.id.username_scroll);
ImageView userdetail2 = (ImageView) findViewById(R.id.profile_pic_scroll);
userdetail1.setText(userNameMessage);
userdetail2.setImageURI(Uri.parse(userPicMessage));


I have tried passing the uri but getting no results in passing the user pic
also for help here is the code which fetches user profile from Facebook and sets it into the navbar of Activity 1

public void setUserProfile(String jsondata) {

try {
response = new JSONObject(jsondata);
user_email.setText(response.get("email").toString());
user_name.setText(response.get("name").toString());
profile_pic_data = new JSONObject(response.get("picture").toString());
profile_pic_url = new JSONObject(profile_pic_data.getString("data"));

Picasso.with(this).load(profile_pic_url.getString("url"))
.into(user_picture);

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


So please suggest me how can i get the image to activity 2.

Answer

You can pass the image Bitmap through the intent to the second activity as it implements the Parcelable interface.

Bitmap profileBitmap = ((BitmapDrawable)user_picture.getDrawable()).getBitmap();
ridesameIntent.putExtra("profilePic", profileBitmap);

And on the other end :

Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("profilePic");
Comments