Guest User

Untitled

a guest
Jul 17th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. public class TakePhoto extends AppCompatActivity implements View.OnClickListener{
  2.  
  3. private Button button, button2, button3;
  4. private String encoded_string, image_name;
  5. private Bitmap bitmap;
  6. private File file;
  7. private Uri file_uri;
  8. private final int IMG_GALLERY = 1;
  9. private final int IMG_CAMERA = 2;
  10. private ImageView imageView;
  11.  
  12. @Override
  13. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  14. super.onActivityResult(requestCode, resultCode, data);
  15.  
  16. if(requestCode == IMG_GALLERY && resultCode == RESULT_OK && data != null){
  17.  
  18. Uri uri_gallery = data.getData();
  19. try {
  20. bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri_gallery);
  21. imageView.setImageBitmap(bitmap);
  22. } catch (IOException e) {
  23. e.printStackTrace();
  24. }
  25. }else if(requestCode == IMG_CAMERA && resultCode == RESULT_OK && data != null){
  26. Uri uri_camera = data.getData();
  27. try {
  28. bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri_camera);
  29. imageView.setImageBitmap(bitmap);
  30. } catch (IOException e) {
  31. e.printStackTrace();
  32. }
  33.  
  34.  
  35. }
  36. }
  37.  
  38. @Override
  39. protected void onCreate(Bundle savedInstanceState) {
  40.  
  41. super.onCreate(savedInstanceState);
  42. setContentView(R.layout.activity_take_photo);
  43.  
  44. button = (Button) findViewById(R.id.button);
  45. button2 = (Button) findViewById(R.id.button2);
  46. button3 = (Button) findViewById(R.id.button3);
  47. imageView = findViewById(R.id.imageShow);
  48. button.setOnClickListener(this);
  49. button2.setOnClickListener(this);
  50. button3.setOnClickListener(this);
  51. }
  52.  
  53. private void selectImage(){
  54.  
  55. Intent intent = new Intent();
  56. intent.setType("image/*");
  57. intent.setAction(Intent.ACTION_GET_CONTENT);
  58. startActivityForResult(intent, IMG_GALLERY);
  59.  
  60. }
  61.  
  62. private void takeImage(){
  63. Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  64. startActivityForResult(cameraIntent, IMG_CAMERA);
  65. }
  66.  
  67. @Override
  68. public void onClick(View v) {
  69.  
  70. switch (v.getId()){
  71.  
  72. case R.id.button:
  73. selectImage();
  74. break;
  75.  
  76. case R.id.button2:
  77. uploadImage();
  78. break;
  79.  
  80. case R.id.button3:
  81. takeImage();
  82. break;
  83.  
  84. }
  85.  
  86.  
  87. }
  88.  
  89. private String imageToString(Bitmap bitmap){
  90. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  91. bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
  92. byte[] imgBytes = byteArrayOutputStream.toByteArray();
  93. return Base64.encodeToString(imgBytes,Base64.DEFAULT);
  94. }
  95.  
  96. private void uploadImage(){
  97.  
  98. Map<String,String> map = new HashMap<String,String>();
  99. map.put("encoded_string",imageToString(bitmap));
  100. map.put("image_name","newimage");
  101.  
  102. API_Service api_service = Client.getRetrofitInstance().create(API_Service.class);
  103. Call<ResponseBody> call = api_service.sendImage(map);
  104. call.enqueue(new Callback<ResponseBody>() {
  105. @Override
  106. public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
  107. Toast.makeText(TakePhoto.this, ""+response.body().getSuccess(), Toast.LENGTH_SHORT).show();
  108. }
  109.  
  110. @Override
  111. public void onFailure(Call<ResponseBody> call, Throwable t) {
  112. Toast.makeText(TakePhoto.this, "hata", Toast.LENGTH_SHORT).show();
  113. }
  114. });
  115.  
  116. }
Add Comment
Please, Sign In to add comment