简体   繁体   中英

how to get image path as a string and save it to a server

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();
            }
        }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM