AddData.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package com.example.zhulinaapi;
  2. import android.content.Intent;
  3. import android.graphics.Bitmap;
  4. import android.graphics.BitmapFactory;
  5. import android.net.Uri;
  6. import android.os.Build;
  7. import android.os.Bundle;
  8. import android.provider.MediaStore;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.ImageView;
  12. import android.widget.TextView;
  13. import android.widget.Toast;
  14. import androidx.activity.result.ActivityResultLauncher;
  15. import androidx.activity.result.contract.ActivityResultContracts;
  16. import androidx.appcompat.app.AppCompatActivity;
  17. import java.io.ByteArrayOutputStream;
  18. import java.io.InputStream;
  19. import java.util.Base64;
  20. import retrofit2.Call;
  21. import retrofit2.Callback;
  22. import retrofit2.Response;
  23. import retrofit2.Retrofit;
  24. import retrofit2.converter.gson.GsonConverterFactory;
  25. public class AddData extends AppCompatActivity implements View.OnClickListener {
  26. TextView txtday;
  27. TextView txtwotkout;
  28. TextView txttrainer;
  29. ImageView imageView;
  30. String image;
  31. @Override
  32. protected void onCreate(Bundle savedInstanceState) {
  33. super.onCreate(savedInstanceState);
  34. setContentView(R.layout.activity_add_data);
  35. Button btnBack = findViewById(R.id.btnBack);
  36. btnBack.setOnClickListener((view -> {
  37. Intent intent = new Intent(AddData.this, MainActivity.class);
  38. startActivity(intent);
  39. }));
  40. Button btnAdd = findViewById(R.id.btnAdd);
  41. btnAdd.setOnClickListener(this);
  42. txtday = findViewById(R.id.day);
  43. txtday.setOnFocusChangeListener((v, hasFocus) -> {
  44. if (hasFocus)
  45. txtday.setHint(null);
  46. else
  47. txtday.setHint(R.string.day);
  48. });
  49. txtwotkout = findViewById(R.id.wotkout);
  50. txtwotkout.setOnFocusChangeListener((v, hasFocus) -> {
  51. if (hasFocus)
  52. txtwotkout.setHint(null);
  53. else
  54. txtwotkout.setHint(R.string.workout);
  55. });
  56. txttrainer = findViewById(R.id.trainer);
  57. txttrainer.setOnFocusChangeListener((v, hasFocus) -> {
  58. if (hasFocus)
  59. txttrainer.setHint(null);
  60. else
  61. txttrainer.setHint(R.string.trainer);
  62. });
  63. imageView = findViewById(R.id.imageView);
  64. imageView.setOnClickListener(v -> {
  65. Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  66. intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  67. pickImg.launch(intent);
  68. });
  69. }
  70. @Override
  71. public void onClick(View v) {
  72. String day = txtday.getText().toString();
  73. String wotkout = txtwotkout.getText().toString();
  74. String trainer = txttrainer.getText().toString();
  75. switch (v.getId()) {
  76. case R.id.btnAdd:
  77. postData(day, wotkout, trainer, image);
  78. }
  79. }
  80. public static String encodeImage(Bitmap bitmap) {
  81. int prevW = 500;
  82. int prevH = bitmap.getHeight() * prevW / bitmap.getWidth();
  83. Bitmap b = Bitmap.createScaledBitmap(bitmap, prevW, prevH, false);
  84. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  85. b.compress(Bitmap.CompressFormat.JPEG, 50, byteArrayOutputStream);
  86. byte[] bytes = byteArrayOutputStream.toByteArray();
  87. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  88. return Base64.getEncoder().encodeToString(bytes);
  89. }
  90. return "";
  91. }
  92. private void postData(String day, String wotkout, String trainer, String image) {
  93. Retrofit retrofit = new Retrofit.Builder()
  94. .baseUrl("https://ngknn.ru:5001/ngknn/ЖулинаАА/api/")
  95. .addConverterFactory(GsonConverterFactory.create())
  96. .build();
  97. RetrofitAPI retrofitAPI = retrofit.create(RetrofitAPI.class);
  98. Mask mask = new Mask(day, wotkout,
  99. trainer, image);
  100. Call<Mask> call = retrofitAPI.createPost(mask);
  101. call.enqueue(new Callback<Mask>() {
  102. @Override
  103. public void onResponse(Call<Mask> call, Response<Mask> response) {
  104. Toast.makeText(AddData.this, "Информация добавлена", Toast.LENGTH_LONG).show();
  105. txtday.setText("");
  106. txtday.clearFocus();
  107. txtwotkout.setText("");
  108. txtwotkout.clearFocus();
  109. txttrainer.setText("");
  110. txttrainer.clearFocus();
  111. }
  112. @Override
  113. public void onFailure(Call<Mask> call, Throwable t) {
  114. Toast.makeText(AddData.this, "Ошибка: " + t.getMessage(), Toast.LENGTH_LONG).show();
  115. }
  116. });
  117. }
  118. private final ActivityResultLauncher<Intent> pickImg = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
  119. if (result.getResultCode() == RESULT_OK) {
  120. if (result.getData() != null) {
  121. Uri uri = result.getData().getData();
  122. try {
  123. InputStream is = getContentResolver().openInputStream(uri);
  124. Bitmap bitmap = BitmapFactory.decodeStream(is);
  125. imageView.setImageBitmap(bitmap);
  126. image = encodeImage(bitmap);
  127. } catch (Exception e) {
  128. e.printStackTrace();
  129. }
  130. }
  131. }
  132. });
  133. }