[英]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.