我将图像保存在数据库中,但未在其他活动中显示。 另外,我只能在图库中保存一张图像。当我第二次拍摄照片时,它将不会替换第一张图像。 现在它访问相机,当我单击按钮选择图片时,它将带我到图库,但仅显示第一张图片。我需要删除第一张图片以替换第二张图片,然后将图片插入下一个条目。 目标是将图像与在数据库中输入的数据一起插入数据库,并显示包含信息的图像。 Deletelayout.java
ImageView imageChoose;
Intent j,c,a;
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.deletelayout);
GoalDate = findViewById(R.id.goalDate2);
GoalWeight = findViewById(R.id.weighGoal2);
CurrentWeight = findViewById(R.id.weightCurrent2);
goalD = findViewById(R.id.goalDateInput);
goalW = findViewById(R.id.goalWeightInput);
currentW = findViewById(R.id.currentWeightInput);
imageChoose = findViewById(R.id.imageShow);
data = myDB.getListContents();
deleteB = findViewById(R.id.deleteB);
updateB = findViewById(R.id.updateButton);
// Entered information from the user are set to the respective variables.
int numRows = data.getCount();
j = new Intent(DeleteLayout.this, historyActivity.class);
if (numRows == 0) {
Toast.makeText(DeleteLayout.this, "Nothing in the db", Toast.LENGTH_LONG).show();
} else {
int i = 0;
while (data.moveToNext()) {
user = new User(data.getString(1), data.getString(2), data.getString(3), data.getString(4), data.getString(5));
gWt = user.getGoalWeight();
cWt = user.getCurrentWeight();
gDt = user.getGoalDate();
GoalDate.setText(gDt);
GoalWeight.setText(gWt);
CurrentWeight.setText(cWt);
deleteB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myDB.deleteContent(GoalDate.getText().toString(), GoalWeight.getText().toString(), CurrentWeight.getText().toString());
startActivity(j);
}
});
updateB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("Goal D:", goalD.getText().toString());
myDB.updateData(GoalDate.getText().toString(), GoalWeight.getText().toString(), CurrentWeight.getText().toString(),goalD.getText().toString(), goalW.getText().toString(), currentW.getText().toString());
Toast.makeText(DeleteLayout.this, "Data updated", Toast.LENGTH_SHORT).show();
startActivity(j);
}
});
}
}
}
Settings.java
chooseImagebutton = findViewById(R.id.button_choose);
chooseImagebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ActivityCompat.requestPermissions(settingsActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_CODE_GALLERY);
}
});
myDB = new DatabaseHelper(this);
//invokes all the info to be saved and displayed in the History and Home Page
saveButton = findViewById(R.id.saveButton);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gDInput = goalDinput.getText().toString();
gWInput = goalWinput.getText().toString();
cWInput = currentWinput.getText().toString();
fInput = ftInput.getText().toString();
iInput = inInput.getText().toString();
if (genderMale.isChecked()) {
gInput = genderMale.getText().toString();
} else {
gInput = genderFemale.getText().toString();
}
if (gDInput.length() != 0 && gWInput.length() != 0 && cWInput.length() != 0
&& gInput.length() != 0 && fInput.length() != 0 && iInput.length() != 0) {
addData(gDInput, gWInput, cWInput, fInput, iInput, imageViewToByte(imageChoose));
goalDinput.setText("");
goalWinput.setText("");
currentWinput.setText("");
ftInput.setText("");
inInput.setText("");
if (gInput == "Male") {
genderMale.setText("");
} else {
genderFemale.setText("");
}
imageChoose.setImageResource(R.mipmap.ic_launcher);
} else {
Toast.makeText(settingsActivity.this, "All fields need to be entered.", Toast.LENGTH_SHORT).show();
}
}
});
}
//method to show message if data inserted or not
private void addData(String gDInput, String gWInput, String cWInput, String Feet, String Inches, byte[] image) {
boolean insertData = myDB.addData(gDInput, gWInput, cWInput, Feet, Inches, image);
if (insertData == true) {
Toast.makeText(settingsActivity.this, "Data inserted successfully", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(settingsActivity.this, "Data not inserted", Toast.LENGTH_SHORT).show();
}
}