无法将图像从一个 activity 发送到另一个。请查看详情

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

我正在从 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);
    }

}

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

在您的 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);

这不是在同一应用程序中将图像从 Activity 传递到 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);

对我有用。

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