I am making a uni project and I chose to make an android app that also needs to upload and retrieve images.
The part I don't understand is the uploading and retrieving part.
I have tried many things from trying to convert the bitmap to bytes and uploading this way or making a path but with no luck, and no matter how much I google, I only get results using weird API's or some firebase stuff.
I have tried this: https://www.maxester.com/blog/2019/10/04/upload-file-image-to-the-server-using-volley-in-android/
and this: https://www.youtube.com/watch?v=ULHyRwep3EU
And a couple of other things which didn't work.
I have a test app that uses this code:
 public void openGallery(){
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Imgae"), PICK_IMAGE);
    }
    public void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == PICK_IMAGE && resultCode == RESULT_OK && data != null){
            Uri pickedImage = data.getData();
            String[] filePath = {MediaStore.Images.Media.DATA};
            Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
            cursor.moveToFirst();
            String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            Bitmap bmp = BitmapFactory.decodeFile(imagePath, options);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] byteArray = stream.toByteArray();
            bmp.recycle();
            cursor.close();
            String url = "https://192.168.108.2/test/upload.php";
            StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Toast.makeText(MainActivity.this, response.toString(), Toast.LENGTH_LONG);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(MainActivity.this, error.getMessage().toString(), Toast.LENGTH_LONG);
                }
            }) {
                protected Map<String, String> getParams(){
                    Map<String, String> map = new HashMap<String, String>();
                    map.put("image", byteArray.toString());
                    return map;
                }
            };
        }
    }
and the php is:
    $username = "root";
    $password = "";
    $dbname = "test";
    $servername = "localhost";
    
    $image = base64_decode($_POST['image']);
    
    $conn = new mysqli($servername, $username, $password, $dbname);
    $conn->set_charset("utf8");
    
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    $sql = "INSERT INTO image(img) VALUES('".$image."');";
    
    if($conn->query($sql)){
        echo "Success";
    }
    else{
        echo "Failed";
    }
?>
I also have a DB with an image field which is of type MEDIUMBLOB with a size of 5mil.
This obviously doesn't work (app crashes when an image is selected). I want someone to walk me through how to upload images to sql via android and how to retrieve them since google isn't helping one bit.
Little update:
So I looked around and I have gotten somewhere but still no image uploading. I now have this code in Android:
public class MainActivity extends AppCompatActivity {
    Button uploadBtn, retrieveBtn;
    ImageView image;
    private static final int PICK_IMAGE = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        image = findViewById(R.id.image);
        uploadBtn = findViewById(R.id.uploadBtn);
        retrieveBtn = findViewById(R.id.retrieveBtn);
        uploadBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openGallery();
            }
        });
        retrieveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getImage();
            }
        });
    }
    public void getImage(){
    }
    public void openGallery(){
        Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
        photoPickerIntent.setType("image/*");
        startActivityForResult(photoPickerIntent, PICK_IMAGE);
    }
    protected void onActivityResult(int reqCode, int resultCode, Intent data) {
        super.onActivityResult(reqCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            try {
                final Uri imageUri = data.getData();
                final InputStream imageStream = getContentResolver().openInputStream(imageUri);
                final Bitmap bmp = BitmapFactory.decodeStream(imageStream);
                image.setImageBitmap(bmp);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                byte[] imageBytes = baos.toByteArray();
                String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
                //Toast.makeText(getApplicationContext(),"ByteArray created..",Toast.LENGTH_SHORT).show();
                String url = "http://192.168.108.2/test/upload.php";
                StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_LONG);
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(getApplicationContext(), error.getMessage().toString(), Toast.LENGTH_LONG);
                    }
                }){
                    protected Map<String, String> getParams(){
                        Toast.makeText(getApplicationContext(), "I'm in stringrequest", Toast.LENGTH_LONG);
                        Map<String, String> map = new HashMap<String, String>();
                        map.put("image", encodedImage.toString());
                        return map;
                    }
                };
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                Toast.makeText(MainActivity.this, "Something went wrong", Toast.LENGTH_LONG).show();
            }
        }else {
            Toast.makeText(MainActivity.this, "You haven't picked Image",Toast.LENGTH_LONG).show();
        }
    }
}
And this php code:
<?php
    $username = "root";
    $password = "";
    $dbname = "test";
    $servername = "localhost";
    $path = './images/';
    $uploadfile = $path . basename($_FILES['image']['name']);
    $echo "I'm in php";
    $conn = new mysqli($servername, $username, $password, $dbname);
    $conn->set_charset("utf8");
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    
    if(file_exists($path)){
        if(move_uploaded_file($_FILES['image']['name'], $uploadfile)){
            $sql = "INSERT INTO image(img) VALUES('".$uploadfile."');";
            echo "Success!";
        }
        else{
            echo "Failed";
        }
    }
    else{
        mkdir($path);
        if(move_uploaded_file($_FILES['image']['name'], $uploadfile)){
            $sql = "INSERT INTO image(img) VALUES('".$uploadfile."');";
            echo "Success!";
        }
        else{
            echo "Failed";
        }
    }
    
?>
When I choose an image, it selects it and puts it in the imageview as it should, but it doesn't upload anything to the SQL. What is wrong?
Forgot to mention: the 'img' field in the SQL table is a size 300 varchar
 
    