1

I have an api that aceepts the $_FILES['profile_image'] File.

I have an android application that enables the user to select a image from their external media storage and they can then submit it along with many other editText fields etc to the API.

How can I pass the selected image to the API?

Here is some of my code:

$return = array();
switch($_REQUEST['action']){
    case 'register':

        $v = true;
        $fields =   array('u_first_name', 'u_last_name');
        foreach($fields as $f)
            ${$f} = $_REQUEST[$f];

        $image_file = $_FILES['profile_image'];

    //validate, upload image and manipulate db
}

The current android/java code I have is:

submit_button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            Boolean f;
            Boolean i;

            //Get All Fields
            Edi_First_Name          = (EditText)findViewById(R.id.Edi_First_Name);
            Edi_Last_Name           = (EditText)findViewById(R.id.Edi_Last_Name);
            image_preview           = (ImageView)findViewById(R.id.Reg_Profile_Image_Preview);

            //check if any fields are empty, if not proceed to api, else show errors
            f = Function.notEmpty("First Name", Edi_First_Name);
            f &= Function.notEmpty("Last Name", Edi_Last_Name);

            //check if image has been set
            i = Function.imageNotSet(image_preview);
            f &= i;

            if(f == true){
                //if everything is validated,talk to api.

                String first_name, last_name, company_name, job_title, email, contact, address_one, address_two, town, county, postcode, country, account_email, rep_account_email, account_password, rep_account_password;

                first_name          = Function.getEditText(Edi_First_Name);
                last_name           = Function.getEditText(Edi_Last_Name);

                //pass parameters so that we can call the register api via FUNCTIONS.java
                Function.registerCall(first_name, last_name, "test_image");
            }
            else{
                Toast.makeText(getApplicationContext(), "Error.", Toast.LENGTH_SHORT).show();
            }
        }
    });

So looking at the code, how can I pass the image from the imagview as a parameter in the registerCall Function? This function then sends a http request to the API.

Can anyone offer me a solution?

Thanks

1

1 Answer 1

0

Although Im not familiar with the PHP, maybe I can point you in the right direction.

First thing is that when user selects his image from the storage, not only use it to set the contents of the ImageView, but also keep the path to the image (for example as a field of the activity).

Then when the call to the api should be made - use the saved path to the file.

The way you should handle the path you can probably take from here (<- textFile is your path).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.