Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TakePhoto extends AppCompatActivity implements View.OnClickListener{
- private Button button, button2, button3;
- private String encoded_string, image_name;
- private Bitmap bitmap;
- private File file;
- private Uri file_uri;
- private final int IMG_GALLERY = 1;
- private final int IMG_CAMERA = 2;
- private ImageView imageView;
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if(requestCode == IMG_GALLERY && resultCode == RESULT_OK && data != null){
- Uri uri_gallery = data.getData();
- try {
- bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri_gallery);
- imageView.setImageBitmap(bitmap);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }else if(requestCode == IMG_CAMERA && resultCode == RESULT_OK && data != null){
- Uri uri_camera = data.getData();
- try {
- bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri_camera);
- imageView.setImageBitmap(bitmap);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_take_photo);
- button = (Button) findViewById(R.id.button);
- button2 = (Button) findViewById(R.id.button2);
- button3 = (Button) findViewById(R.id.button3);
- imageView = findViewById(R.id.imageShow);
- button.setOnClickListener(this);
- button2.setOnClickListener(this);
- button3.setOnClickListener(this);
- }
- private void selectImage(){
- Intent intent = new Intent();
- intent.setType("image/*");
- intent.setAction(Intent.ACTION_GET_CONTENT);
- startActivityForResult(intent, IMG_GALLERY);
- }
- private void takeImage(){
- Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
- startActivityForResult(cameraIntent, IMG_CAMERA);
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()){
- case R.id.button:
- selectImage();
- break;
- case R.id.button2:
- uploadImage();
- break;
- case R.id.button3:
- takeImage();
- break;
- }
- }
- private String imageToString(Bitmap bitmap){
- ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
- bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
- byte[] imgBytes = byteArrayOutputStream.toByteArray();
- return Base64.encodeToString(imgBytes,Base64.DEFAULT);
- }
- private void uploadImage(){
- Map<String,String> map = new HashMap<String,String>();
- map.put("encoded_string",imageToString(bitmap));
- map.put("image_name","newimage");
- API_Service api_service = Client.getRetrofitInstance().create(API_Service.class);
- Call<ResponseBody> call = api_service.sendImage(map);
- call.enqueue(new Callback<ResponseBody>() {
- @Override
- public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
- Toast.makeText(TakePhoto.this, ""+response.body().getSuccess(), Toast.LENGTH_SHORT).show();
- }
- @Override
- public void onFailure(Call<ResponseBody> call, Throwable t) {
- Toast.makeText(TakePhoto.this, "hata", Toast.LENGTH_SHORT).show();
- }
- });
- }
Add Comment
Please, Sign In to add comment