Hi Guys I’m struggling for a couple of days with my Kotlin Code to upload a picture from my phone to the server but my request works only with Postman so I don’t know what I’m doing wrong I will appreciate any help: This my API.php
<?php
//Constants for database connection
define('DB_HOST','localhost');
define('DB_USER','root');
define('DB_PASS','');
define('DB_NAME','ptb');
//We will upload files to this folder
//So one thing don't forget, also create a folder named uploads inside your project folder i.e. MyApi folder
define("UPLOAD_PATH", "/WebApi/uploads/");
//connecting to database
$conn = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME) or die('Unable to connect');
$target_dir = "uploads/";
$filepath = $target_dir . basename($_FILES['pic']['name']);
//An array to display the response
$response = array();
//if the call is an api call
if(isset($_GET['apicall'])){
//switching the api call
switch($_GET['apicall']){
//if it is an upload call we will upload the image
case 'uploadpic':
//first confirming that we have the image and tags in the request parameter
if(isset($_FILES['pic']['name']) ){
//uploading file and storing it to database as well
try{
move_uploaded_file($_FILES['pic']['tmp_name'], $filepath);
$stmt = $conn->prepare("INSERT INTO images (image, tags) VALUES (?,'s')");
$stmt->bind_param("s", $_FILES['pic']['name']);
if($stmt->execute()){
$response['error'] = false;
$response['message'] = UPLOAD_PATH . $_FILES['pic']['name'];
}else{
throw new Exception("Could not upload file");
}
}catch(Exception $e){
$response['error'] = true;
$response['message'] = 'Could not upload file';
}
}else{
$response['error'] = true;
$response['message'] = "Required params not available";
}
break;
This Main.kt with pic_save_api as http://x.x.x.x/WebApi/includes/api.php?apicall=uploadpic
private fun launchGallery() {
val intent = Intent(Intent.ACTION_PICK)
intent.type = "image/*"
startActivityForResult(intent, IMAGE_PICK_CODE)
}
private fun uploadImage() {
val stringRequest = object : StringRequest(Request.Method.POST, EndPoints.pic_save_api,
Response.Listener<String> { response ->
try {
println("response")
Toast.makeText(applicationContext, response, Toast.LENGTH_LONG).show()
} catch (e: JSONException) {
e.printStackTrace()
}
},
object : Response.ErrorListener {
override fun onErrorResponse(volleyError: VolleyError) {
Toast.makeText(applicationContext, volleyError.message, Toast.LENGTH_LONG).show()
}
})
{
@Throws(AuthFailureError::class)
fun getByteData(): MutableMap<String, FileDataPart> {
var params = HashMap<String, FileDataPart>()
params.put("tags" , "test")
params.put("pic" , FileDataPart("image", imageData!!, "jpg"))
return params
}
}
Volley.newRequestQueue(this).add(stringRequest)
}
@Throws(IOException::class)
private fun createImageData(uri: Uri) {
val inputStream = contentResolver.openInputStream(uri)
inputStream?.buffered()?.use {
imageData = it.readBytes()
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (resultCode == Activity.RESULT_OK && requestCode == IMAGE_PICK_CODE) {
val uri = data?.data
if (uri != null) {
imageView.setImageURI(uri)
createImageData(uri)
}
}
super.onActivityResult(requestCode, resultCode, data)
}
this is how I call the two functions
imageButton.setOnClickListener { launchGallery() }
sendButton.setOnClickListener { uploadImage() }
I will appreciate any help!