0

I want to upload image from android app but it generate error of Undefined Index:user_pic in xampp\htdocs\CityHero\user_registration.php on line 13...
I can't understand what is the problem related to this error..
user_registration.php

    <?php
require "config.php";

$obj = new config();

$er = 0;
$filename = ""; 

$return = array();

if (isset($_POST["user_name"]) && isset($_POST["user_email"]) && isset($_POST["user_city"]) && isset($_POST["user_area"]) && isset($_POST["username"]) && isset($_POST["password"])) {

    if ($_FILES['user_pic']["name"] != "") {
        $type = "." . pathinfo($_FILES['user_pic']['name'],PATHINFO_EXTENSION);

        if ($type == ".jpeg" || $type == ".png" || $type == ".jpg" || $type == ".bmp") {
            $filename = date("d-m-Y-H-i-s").$type;
            $file_server_path = $obj->SERVERPATH . basename($filename);
            move_uploaded_file($_FILES['user_pic']['tmp_name'], $file_server_path);

            $return["STATUS"] = true;
            $return["MSG"] = "FILE UPLOADED SUCCESSFULLY...";
        }else{
            $er = 1;
            echo "Error!!!Please Check Code Once Again....";
            // echo $return["STATUS"] = false;
            // echo $return["MSG"] = "ERROR!!! Please Check Code once again....";
        }

        if ($er == 0) {
            $insData["user_name"] = $_POST["user_name"];
            $insData["user_email"] = $_POST["user_email"];
            $insData["user_city"] = $_POST["user_city"];
            $insData["user_area"] = $_POST["user_area"];
            $insData["username"] = $_POST["username"];
            $insData["password"] = md5($_POST["password"]);
            $insData["create_date"] = $obj->current_date;
            $insData["modify_date"] = $obj->current_date;
            $insData["remote_ip"] = $obj->remote_ip;
            $insData["user_pic"] = $filename;

            $result = $obj->myInsdata("users_registration",$insData);

            if ($result) {
                echo "Insertion Succesfull";
                // echo $return["STATUS"] = true;
                // echo $return["MSG"] = "INSERTED SUCCESSFULLY...";                
            }else{
                echo "Error!!!Please Check Code Once Again....";
                // echo $return["STATUS"] = false;
                // echo $return["MSG"] = "INSERTED NOT SUCCESSFULL...";     
            }
        }
    }
}else{
    echo "Error!!!Please Check Code Once Again....";
    // echo $return["STATUS"] = false;
    // echo $return["MSG"] = "FIELD IS NOT SET..";      
}

//echo json_encode($return);

?>

And here is my Android Studio Upload Code..

  package com.example.acer.jsonparsing;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    EditText fname,femail,fcity,farea,fusername,password,path;

    Button insert,upload;

    String insertURL = "http://192.168.2.76/CityHero/user_registration.php";

    RequestQueue requestQueue;

    String encodedImage;

    Bitmap bitmap;

    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fname = (EditText)findViewById(R.id.fname);
        femail = (EditText)findViewById(R.id.email);
        fcity = (EditText)findViewById(R.id.city);
        farea = (EditText)findViewById(R.id.area);
        fusername = (EditText)findViewById(R.id.username);
        password = (EditText)findViewById(R.id.pass);
        upload=(Button)findViewById(R.id.uploadimage);
        insert = (Button)findViewById(R.id.insertdata);
        imageView=(ImageView)findViewById(R.id.image);

        upload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent galleryIntent=new Intent();
                galleryIntent.setType("image/*");
                galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(galleryIntent,123);
            }
        });


        requestQueue = Volley.newRequestQueue(MainActivity.this);

        insert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringRequest request = new StringRequest(Request.Method.POST, insertURL, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Toast.makeText(MainActivity.this, response, Toast.LENGTH_LONG).show();
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                        Toast.makeText(MainActivity.this,error.toString(), Toast.LENGTH_SHORT).show();

                    }
                }){
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String,String> parameters = new HashMap<>();
                        String imageData = encodedImage;
                        parameters.put("user_name",fname.getText().toString());
                        parameters.put("user_email",femail.getText().toString());
                        parameters.put("user_city",fcity.getText().toString());
                        parameters.put("user_area",farea.getText().toString());
                        parameters.put("username",fusername.getText().toString());
                        parameters.put("password",password.getText().toString());
                        parameters.put("user_pic",encodedImage);
                        return parameters;
                    }
                };
                requestQueue.add(request);
            }
        });
    }

    private String getImageString(Bitmap bitmap ){
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG,100,outputStream);
        byte[] imagebyte = outputStream.toByteArray();

        encodedImage = Base64.encodeToString(imagebyte,Base64.DEFAULT);
        return encodedImage;

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode)
        {
            case 123:
                Uri imageuri = data.getData();

                try {
                    bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),imageuri);
                    getImageString(bitmap);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                imageView.setImageBitmap(bitmap);
                break;
        }
    }
}

Please Help me how can i upload image from Android Application to Mysql database...

6
  • you should pass a File naming user_pic Commented Oct 30, 2018 at 10:04
  • At which place..? Commented Oct 30, 2018 at 10:05
  • you need Multipart to upload File to sever. Commented Oct 30, 2018 at 10:07
  • See here it will help you. Commented Oct 30, 2018 at 10:08
  • ok....is there any other way to upload image on server? Commented Oct 30, 2018 at 10:13

1 Answer 1

0

You can upload pic using Gotev Upload Service

In gradle dependencies

dependencies {  
   implementation "net.gotev:uploadservice:3.4.2"
}

Then in Activity

public void uploadMultipart(final Context context) {    
    try {  
        String uploadId =  
                new MultipartUploadRequest(context, "your URL here")  
                        .addFileToUpload("data.getData() here", "user_pic")                           
                        .addParameter("user_name", "param value") 
                        .addParameter("user_email", "param value") 
                        .addParameter("user_city", "param value") 
                        .addParameter("user_area", "param value") 
                        .addParameter("username", "param value")  
                        .addParameter("password", "param value")        
                        .setNotificationConfig(new UploadNotificationConfig())  
                            .setMaxRetries(2)  
                            .startUpload();  

        } catch (Exception exc) {  
            Toast.makeText(context, "Exception: "+exc.getMessage(), Toast.LENGTH_SHORT).show();  
        }  
    } 
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.