无法将图像从一个活动发送到另一个活动.请查看详细信息



我正在从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)可能会被调用。首先要确保你已经从facebook上获取了图片,然后再调用startActivity(profileIntent)

编辑

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

最新更新