5.31

public class MoodRecordActivity extends AppCompatActivity {

private String age;
private String beauty;
private String expression;
private String imageBase64;
private EditText editTextMood;
private ImageView imageViewResult;
private ExecutorService executorService = Executors.newSingleThreadExecutor();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mood_record);

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Mood Record");
toolbar.setNavigationOnClickListener(view -> finish());

TextView textViewResult = findViewById(R.id.textView_result);
editTextMood = findViewById(R.id.editText_mood);
imageViewResult = findViewById(R.id.imageView_result);

age = getIntent().getStringExtra("age");
beauty = getIntent().getStringExtra("beauty");
expression = getIntent().getStringExtra("expression");
imageBase64 = getIntent().getStringExtra("image");

String resultText = "Age: " + age + "\nBeauty: " + beauty + "\nExpression: " + expression;
textViewResult.setText(resultText);

byte[] imageBytes = android.util.Base64.decode(imageBase64, android.util.Base64.DEFAULT);
imageViewResult.setImageBitmap(android.graphics.BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_upload, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_upload) {
uploadMoodRecord();
return true;
}
return super.onOptionsItemSelected(item);
}

private void uploadMoodRecord() {
String moodText = editTextMood.getText().toString();
Log.d("MoodRecordActivity", "Uploading record: Age=" + age + ", Beauty=" + beauty + ", Expression=" + expression + ", Mood=" + moodText);

executorService.execute(() -> {
try {
Connection connection = JDBCUtils.getConn();
if (connection != null) {
Log.d("MoodRecordActivity", "Database connection established");
String sql = "INSERT INTO mood_records (age, beauty, expression, image, mood, date) VALUES (?, ?, ?, ?, ?, NOW())";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, age);
preparedStatement.setString(2, beauty);
preparedStatement.setString(3, expression);
preparedStatement.setString(4, imageBase64);
preparedStatement.setString(5, moodText);
preparedStatement.executeUpdate();
preparedStatement.close();
connection.close();
runOnUiThread(() -> {
Toast.makeText(MoodRecordActivity.this, "Record uploaded successfully", Toast.LENGTH_SHORT).show();
finish(); // 返回到主页面
});
} else {
Log.e("MoodRecordActivity", "Failed to connect to database");
}
} catch (SQLException e) {
Log.e("MoodRecordActivity", "SQL Exception: " + e.getMessage());
}
});
}
}
posted @ 2024-06-04 22:04  涨涨涨张  阅读(5)  评论(0编辑  收藏  举报