0

I am working on a project that needs to save user's information including the profile image to the server using retrofit. the backend was built with java spring boot and the profile image was declared as a string in the backend. I want to pick an image from my gallery by using a crop image and save the image to the server so that I can retrieve it anytime from the server and upload it when the user logins. I am still a beginner in android app development so I don't know how to go about that. I have picked and cropped the image successfully but the image path cannot be formatted to a string that can be stored and retrieved anytime. I really need help here. Thanks

    Here is the code.
    
    imagePicker.setOnClickListener(view -> {
                CropImage.activity()
                        .setAspectRatio(1,1)
                        .start(PatientRegistrationActivity.this);
            });
    
     @Override
        protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode==CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE && resultCode==RESULT_OK && data !=null){
                CropImage.ActivityResult result = CropImage.getActivityResult(data);
                Uri profileImageUri = result.getUri();
                profilePicture.setImageURI(profileImageUri);//profilePicture here is imageview that displays the picked image from the gallery
                  String imagePath= profileImageUri.getPath();
                  Log.d("Main","Your image path is "+imagePath);
            }else {
                Toast.makeText(PatientRegistrationActivity.this,"Error Occurred, Please try Again",Toast.LENGTH_SHORT).show();
            }
        }
Ukeme Elijah
  • 157
  • 3
  • 13
  • Use Base64 to convert image to a string. – grabarz121 Sep 14 '22 at 13:30
  • `the image successfully but the image path cannot be formatted to a string that can be stored and retrieved anytime.` Of course it can. As if you have an image path you would have it as string already. Nothing to format. – blackapps Sep 14 '22 at 14:41
  • I think you mean you want format image to a string. Not the parh. Well you can base64 encode de bytes of an image file to a string. – blackapps Sep 14 '22 at 14:43
  • `String imagePath= profileImageUri.getPath();` Sorry but that is no file system path. – blackapps Sep 14 '22 at 14:46

1 Answers1

0

You can convert the image to string and upload to the server. You can later convert the the string that you get from the server back to the image.

you can refer this for the converting between image and string

  • I have gone through the link, it will be helpful though but is there no way to convert it to string and upload it so that when I want to retrieve it, I can only retrieve it directly and upload it to the imageview using either Picasso or Glide without having to decode it @Rakshit Tanti? – Ukeme Elijah Sep 14 '22 at 13:45
  • Strange that base64 encoding/decoding is not even mentioned. – blackapps Sep 14 '22 at 14:44