无法将图像从一个活动发送到另一个活动。请看细节

时间:2016-01-02 13:42:40

标签: java android facebook-graph-api facebook-android-sdk facebook-sdk-4.0

我正在从Facebook获取用户的个人资料图片,我想将其发送到ProfileActivity.java,以便可以在用户个人资料中显示。

问题是图片未从SignUpScreen.java发送到ProfileActivity.java。虽然我能够发送姓名&从一个到另一个的电子邮件。

此处的SignUpScreen.java文件代码:

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

    }
}

此处的ProfileActivity.java文件代码:

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

}

请告诉我这里出了什么问题。

3 个答案:

答案 0 :(得分:0)

getUserDetailsFromFacebook()方法中,您调用了new ProfilePicAsync().execute(userID)来获取图片。但似乎在你可以获取图像之前,startActivity(profileIntent)可能会被调用。 首先确保在致电startActivity(profileIntent)之前已从Facebook获取图像。

修改

将此添加到您的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);

将此添加到您的ProfileActivity.java

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

答案 1 :(得分:0)

这不是在同一应用程序中将图像从Activity传递到Activity的正确方法。您可以轻松地按意图发送路径并将其加载到其他活动中。

要在Activity A中保存位图,请使用

FileOutputStream out = null;
try {
    out = new FileOutputStream(FILENAME); //FILENAME is your defined place to store image
    bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (Exception e) {
    e.printStackTrace();
} finally {
    try {
        if (out != null) {
            out.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

现在您有FILENAME个全局字符串,可从Activity B访问。 只需将其加载到需要的地方。

BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(FILENAME, options);
mProfileImage.setImageBitmap(bitmap);

答案 2 :(得分:0)

它对我有用。

<强> OneActivity.java

ByteArrayOutputStream stream = new ByteArrayOutputStream();
                        bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
                        byte[] byteArray = stream.toByteArray();

                        Intent intent = new Intent(StartPage.this, SecondActivity.class);
                        Toast.makeText(StartPage.this, "You have setted this wallpaper for Monday", Toast.LENGTH_LONG).show();
                        intent.putExtra("pic", byteArray);
                        //intent.putExtra("resourseInt", bm);
                        startActivity(intent);

<强> SecondActivity.Java

byte[] byteArray;
Bitmap bmp,
byteArray = getIntent().getByteArrayExtra("pic");
            bmp1 = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

myWallpaperManager.setBitmap(bmp);