简体   繁体   中英

How to retrieve a picture inserted in the database from 1st to 2nd activity?

I can't retrieve the image from a database in my second activity.

     import android.content.Intent;
            import android.support.v7.app.AppCompatActivity;
            import android.os.Bundle;
            import android.view.View;
            import android.widget.Button;
            import android.widget.ImageView;
            import android.widget.TextView;

            import com.example.dell.egyptholidayapp.Helper.DatabaseHelper;
            import com.example.dell.egyptholidayapp.R;

            public class MyAccountPageActivity extends AppCompatActivity {

                private TextView Username, Email, Nationality, Gender, Age, Mobile;
                private Button Edit;

                private ImageView ivImage2;

                String user, email, nationality, gender, age, mobile;

                Bundle bundle;

                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_my_account_page);
                    bundle = getIntent().getExtras();
                    if (bundle != null){
                        int resId = bundle.getInt("resId");
                        ivImage2.setImageResource(resId);
                    }

                    user = getIntent().getExtras().getString("NAME");
                    email = getIntent().getExtras().getString("EMAIL");
                    nationality = getIntent().getExtras().getString("NATIONALITY");
                    gender = getIntent().getExtras().getString("GENDER");
                    age = getIntent().getExtras().getString("AGE");
                    mobile = getIntent().getExtras().getString("MOBILE");

                    ivImage2 = findViewById(R.id.ivImage2);

                    Username = findViewById(R.id.username_v);
                    Email = findViewById(R.id.email_v);
                    Nationality = findViewById(R.id.nationality_v);
                    Gender = findViewById(R.id.gender_v);
                    Age = findViewById(R.id.age_v);
                    Mobile = findViewById(R.id.mobile_v);
                    Edit = findViewById(R.id.edit_btn);

                    Username.setText(" " + user);
                    Email.setText(" " + email);
                    Nationality.setText(" " + nationality);
                    Gender.setText(" " + gender);
                    Age.setText(" " + age);
                    Mobile.setText(" " + mobile);

                    Edit.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(MyAccountPageActivity.this, EditMyAccountActivity.class);
                            startActivity(intent);
                        }
                    });



                }
            }

Your code is alright, considering that the IDs you are sending from Activity1 to Activity2 are correct, the only problem with your code is that you are using ivImage2 without initializing it first so what you have to do first is find the image view then use it to set the image you sent from Activity1, your code should be like this:

                setContentView(R.layout.activity_my_account_page);
                //You should move this line here and everything will work as you want it to
                ivImage2 = findViewById(R.id.ivImage2);

                bundle = getIntent().getExtras();
                if (bundle != null){
                    int resId = bundle.getInt("resId");
                    ivImage2.setImageResource(resId);
                }

                user = getIntent().getExtras().getString("NAME");
                email = getIntent().getExtras().getString("EMAIL");
                nationality = getIntent().getExtras().getString("NATIONALITY");
                gender = getIntent().getExtras().getString("GENDER");
                age = getIntent().getExtras().getString("AGE");
                mobile = getIntent().getExtras().getString("MOBILE");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM