如何从导航栏传递用户图像(获取来自Facebook的个人主页)到另一个活动

问题描述:

这是我的活动1如何从导航栏传递用户图像(获取来自Facebook的个人主页)到另一个活动

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); 

这是活动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)); 

我试图传递的URI,但获得没有结果传递用户图片 也在这里寻求帮助是从Facebook获取用户个人资料并将其设置到活动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(); 
    } 
} 

所以,请建议我我如何能得到图像的活动2.

profile_pic_url.getString("url")提取这个值到一个变量,并把它作为你的意图额外的,然后在setUserProfile在你的第二个活动与毕加索加载它像方法。

编辑。 因此,在您的load()方法中,您在此行的setUserProfile(String jsondata)方法Picasso.with(this).load(profile_pic_url.getString("url")).into(user_picture);中,通过调用profile_pic_url.getString("url")JSONObject中提取url字符串。您可以从通过右键单击该语句创建一个字段 - >重构 - >提取物 - >场......然后打电话把它作为额外给你的意图,在你的第二个电话的活动呼吁ridesameIntent.putExtra(EXTRA_MESSAGE_USERPIC, fieldNameThatYouExtracted);

然后 Picasso.with(this).load(userPicMessage).into(userPicMessage);代替userdetail2.setImageURI(Uri.parse(userPicMessage));

之后,你可以学习如何使用GSON库Leveraging the Gson Library

+0

你能否用上面的代码解释一下? – Shubhendra

+0

我试过上述方法,但它不工作,任何其他的选择? – Shubhendra

+0

你会得到什么错误? –

简化反序列化JSON你可以通过意图第二活动通过位图图像,因为它实现了Parcelable接口。

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

而在另一端:

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

这真的很有帮助,但是如果我想将图像url发送到json数据库,然后在以后的步骤中使用picasso检索它,那么这个位图的东西没有帮助。 – Shubhendra

你好,这是我的代码由我摆脱这个问题

的在第一项活动

Profile profile = Profile.getCurrentProfile(); 
        Bundle mBundle = new Bundle(); 
          mBundle.putParcelable("pic", profile); 
        Intent loginint = new Intent(MainActivity.this,DashBoard.class); 
        loginint.putExtra("email",email); 
        loginint.putExtras(mBundle); 

        startActivity(loginint); 
        finish(); 

而在二你想表现的活动图片

Intent intent = getIntent(); 
    Bundle mBundle =intent.getExtras(); 
    Profile profile; 

    if (mBundle != null) { 
     profile = (Profile) mBundle.getParcelable("pic"); 
    } else { 
     profile = Profile.getCurrentProfile(); 
    } 

    Picasso.with(DashBoard.this) 
      .load(profile.getProfilePictureUri(400, 400).toString()) 
      .into(mfbimage); 

此处mfbimage是ImageView的对象,您要显示该配置文件图片。