Change.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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.os.Handler;
  9. import android.provider.MediaStore;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.ImageView;
  13. import android.widget.TextView;
  14. import android.widget.Toast;
  15. import androidx.activity.result.ActivityResultLauncher;
  16. import androidx.activity.result.contract.ActivityResultContracts;
  17. import androidx.appcompat.app.AppCompatActivity;
  18. import java.io.ByteArrayOutputStream;
  19. import java.io.InputStream;
  20. import java.util.Base64;
  21. import retrofit2.Call;
  22. import retrofit2.Callback;
  23. import retrofit2.Response;
  24. import retrofit2.Retrofit;
  25. import retrofit2.converter.gson.GsonConverterFactory;
  26. public class Change extends AppCompatActivity implements View.OnClickListener {
  27. TextView txtday;
  28. TextView txtwotkout;
  29. TextView txttrainer;
  30. ImageView imageView;
  31. String image;
  32. int Id;
  33. @Override
  34. protected void onCreate(Bundle savedInstanceState) {
  35. super.onCreate(savedInstanceState);
  36. setContentView(R.layout.activity_change);
  37. Button btnBack = findViewById(R.id.btnBack);
  38. btnBack.setOnClickListener((view -> {
  39. Intent intent = new Intent(Change.this, MainActivity.class);
  40. startActivity(intent);
  41. }));
  42. Button btnSafe = findViewById(R.id.btnSafe);
  43. btnSafe.setOnClickListener(this);
  44. Button btnDel = findViewById(R.id.btnDel);
  45. btnDel.setOnClickListener(this);
  46. txtday = findViewById(R.id.day);
  47. txtday.setOnFocusChangeListener((v, hasFocus) -> {
  48. if (hasFocus)
  49. txtday.setHint(null);
  50. else
  51. txtday.setHint(R.string.day);
  52. });
  53. txtwotkout = findViewById(R.id.wotkout);
  54. txtwotkout.setOnFocusChangeListener((v, hasFocus) -> {
  55. if (hasFocus)
  56. txtwotkout.setHint(null);
  57. else
  58. txtwotkout.setHint(R.string.workout);
  59. });
  60. txttrainer = findViewById(R.id.trainer);
  61. txttrainer.setOnFocusChangeListener((v, hasFocus) -> {
  62. if (hasFocus)
  63. txttrainer.setHint(null);
  64. else
  65. txttrainer.setHint(R.string.trainer);
  66. });
  67. imageView = findViewById(R.id.imageView);
  68. imageView.setOnClickListener(v -> {
  69. Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  70. intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  71. pickImg.launch(intent);
  72. });
  73. setData();
  74. }
  75. @Override
  76. public void onClick(View v) {
  77. switch (v.getId()) {
  78. case R.id.btnSafe:
  79. String day = txtday.getText().toString();
  80. String wotkout = txtwotkout.getText().toString();
  81. String trainer = txttrainer.getText().toString();
  82. putData(Id, day, wotkout, trainer, image);
  83. break;
  84. case R.id.btnDel:
  85. deleteData(Id);
  86. new Handler().postDelayed(() -> startActivity(
  87. new Intent(Change.this, MainActivity.class)), 200);
  88. break;
  89. }
  90. }
  91. private void setData() {
  92. Bundle arg = getIntent().getExtras();
  93. Id = arg.getInt("id");
  94. txtday.setText(arg.getString("day"));
  95. txtwotkout.setText(arg.getString("wotkout"));
  96. txttrainer.setText(arg.getString("trainer"));
  97. image = arg.getString("image");
  98. imageView.setImageBitmap(getImgBitmap(image));
  99. }
  100. private void putData(int Id, String day, String wotkout, String trainer, String image) {
  101. Retrofit retrofit = new Retrofit.Builder()
  102. .baseUrl("https://ngknn.ru:5001/NGKNN/ЖулинаАА/api/")
  103. .addConverterFactory(GsonConverterFactory.create())
  104. .build();
  105. RetrofitAPI retrofitAPI = retrofit.create(RetrofitAPI.class);
  106. Mask mask = new Mask(Id, day, wotkout,
  107. trainer, image);
  108. Call<Mask> call = retrofitAPI.updateData(Id, mask);
  109. call.enqueue(new Callback<Mask>() {
  110. @Override
  111. public void onResponse(Call<Mask> call, Response<Mask> response) {
  112. Toast.makeText(Change.this, "Изменение зафиксировано", Toast.LENGTH_SHORT).show();
  113. }
  114. @Override
  115. public void onFailure(Call<Mask> call, Throwable t) {
  116. Toast.makeText(Change.this, "Ошибка: " + t.getMessage(), Toast.LENGTH_LONG).show();
  117. }
  118. });
  119. }
  120. private void deleteData(int id) {
  121. Retrofit retrofit = new Retrofit.Builder()
  122. .baseUrl("https://ngknn.ru:5001/NGKNN/ЖулинаАА/api/")
  123. .addConverterFactory(GsonConverterFactory.create())
  124. .build();
  125. RetrofitAPI retrofitAPI = retrofit.create(RetrofitAPI.class);
  126. Call<Mask> call = retrofitAPI.deleteData(id);
  127. call.enqueue(new Callback<Mask>() {
  128. @Override
  129. public void onResponse(Call<Mask> call, Response<Mask> response) {
  130. Toast.makeText(Change.this, "Информация удалена", Toast.LENGTH_SHORT).show();
  131. }
  132. @Override
  133. public void onFailure(Call<Mask> call, Throwable t) {
  134. Toast.makeText(Change.this, "Ошибка: " + t.getMessage(), Toast.LENGTH_LONG).show();
  135. }
  136. });
  137. }
  138. public static String encodeImage(Bitmap bitmap) {
  139. int prevW = 500;
  140. int prevH = bitmap.getHeight() * prevW / bitmap.getWidth();
  141. Bitmap b = Bitmap.createScaledBitmap(bitmap, prevW, prevH, false);
  142. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  143. b.compress(Bitmap.CompressFormat.JPEG, 50, byteArrayOutputStream);
  144. byte[] bytes = byteArrayOutputStream.toByteArray();
  145. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  146. return Base64.getEncoder().encodeToString(bytes);
  147. }
  148. return "";
  149. }
  150. private Bitmap getImgBitmap(String encodedImg) {
  151. if (!encodedImg.equals("null")) {
  152. byte[] bytes = new byte[0];
  153. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
  154. bytes = Base64.getDecoder().decode(encodedImg);
  155. }
  156. return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
  157. }
  158. return BitmapFactory.decodeResource(getResources(),
  159. R.drawable.photo);
  160. }
  161. public final ActivityResultLauncher<Intent> pickImg = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
  162. if (result.getResultCode() == RESULT_OK) {
  163. if (result.getData() != null) {
  164. Uri uri = result.getData().getData();
  165. try {
  166. InputStream is = getContentResolver().openInputStream(uri);
  167. Bitmap bitmap = BitmapFactory.decodeStream(is);
  168. imageView.setImageBitmap(bitmap);
  169. image = encodeImage(bitmap);
  170. } catch (Exception e) {
  171. e.printStackTrace();
  172. }
  173. }
  174. }
  175. });
  176. }