简体   繁体   English

如何从第一个活动到第二个活动检索插入数据库中的图片?

[英]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: 考虑到从Activity1发送到Activity2的ID是正确的,因此您的代码没问题,代码的唯一问题是您正在使用ivImage2而不先ivImage2进行初始化,因此首先要做的是找到图像视图然后再使用它要设置您从Activity1发送的图片,您的代码应如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从第一个填充第二个列表框 - populate 2nd listbox from 1st 如何根据从第一个下拉菜单自动更改的第二个下拉菜单更改第二个文本框的值? - How to change the 2nd textbox value based on the 2nd dropdown that is automatically changed from 1st dropdown? 使用来自数据库的javascript值根据1st更改2nd下拉值 - Changing 2nd dropdown values according to 1st using javascript values coming from database 根据在第一个选择框中选择的值,从第二个选择框中的数据库表中选择选项 - Bring the Options from database table in 2nd Select box based on the value selected in 1st Select Box 如果第二个数组匹配,则对第一个数组中的数字求和 - Summing numbers from 1st array if 2nd array match 日期选择器,第二个日期是从第一个日期起的X天 - Datepicker, 2nd date is X days from 1st date 如何搜索表的第二列而不是第一列 - How to search 2nd column of table instead of 1st column 如何在第二次之后停止第一次 function? - How to stop doing 1st function after 2nd? 如何将数据从第二数组添加到第一数组以在Google Visualization API中显示图表 - how to add data from 2nd array to 1st array for showing chart in google Visualization api 如何通过从关联数组中选择项目来基于第一下拉选择绑定第二下拉? - How to bind 2nd dropdown based on 1st dropdown selection by picking item from associative array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM