0

I'm working on how to upload image from android application to server (here I'm using wamp) using volley.For me,images are not saved in mysql database but successfully stored in folder I made?Please help me for rectifying this issue?I"ll post my codes below:

code i used in android:

 public class MainActivity extends AppCompatActivity implements View.OnClickListener {
TextView textView;
Button select,upload;
private final static int  PICK_IMAGE_REQUEST=1;
ImageView imageView;
String FileName,FilePath;
private Bitmap bitmap;
public static final String FILE_UPLOAD_URL="http://192.168.187.1/progressdemo/upload.php";
private String KEY_IMAGE = "image";
private String KEY_NAME = "name";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView=(TextView)findViewById(R.id.textView);
    select=(Button)findViewById(R.id.button);
    upload=(Button)findViewById(R.id.button2);
    imageView=(ImageView)findViewById(R.id.imageView);
    select.setOnClickListener(this);
    upload.setOnClickListener(this);


}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);


            if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

               Uri filepath=data.getData();
                FilePath=filepath.toString();
             FileName=FilePath.substring(FilePath.lastIndexOf("/")+1);
                textView.setText(FileName);
                try {
                    //Getting the Bitmap from Gallery
                    bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filepath);
                    //Setting the Bitmap to ImageView
                    imageView.setImageBitmap(bitmap);
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }


}


@Override
public void onClick(View v) {
    if(v==select)
    {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent,"Select file"),PICK_IMAGE_REQUEST);
    }
    else if (v==upload)
    {
       uploadfile();
    }


}
private void uploadfile()
{
    final ProgressDialog progress=ProgressDialog.show(this,"Uploading image...","....Please wait..",false,false);
    StringRequest stringRequest=new StringRequest(Request.Method.POST, FILE_UPLOAD_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String s) {

                    progress.dismiss();
                    //Showing toast message of the response
                    Toast.makeText(MainActivity.this, s , Toast.LENGTH_LONG).show();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            progress.dismiss();
            //Showing toast message of the response
            Toast.makeText(MainActivity.this, volleyError.toString() , Toast.LENGTH_LONG).show();
        }
    }){
        @Override
        protected Map<String,String> getParams() throws AuthFailureError{
            String name=textView.getText().toString();
            String image=getStringImage(bitmap);
            Map<String,String> params=new Hashtable<String, String>();
            params.put(KEY_IMAGE,image);
            params.put(KEY_NAME,name);
            return params;
        }
    };
    RequestQueue requestQueue= Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}
public String getStringImage(Bitmap bmp)
{

    ByteArrayOutputStream baos=new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG,100,baos);
    byte[] imagebytes=baos.toByteArray();
    String encodedimage= Base64.encodeToString(imagebytes,Base64.DEFAULT);
    return encodedimage;
}
}

codes I used in php:

 <?php

 header('Content-type:bitmap;charset=utf-8');
 if(isset($_POST["image"]))
 {
 $encoded_string=$_POST["image"];
 $name=$_POST["name"];
 $decoded_string=base64_decode($encoded_string);
 $path='images/'.$name;
 $file=fopen($path,'w');
 $is_written=fwrite($file,$decoded_string);
 fclose($file);
 if($is_written>0)
 {
 $con=mysqli_connect('localhost','root','','progressdemo');
 echo "connection success\n";
 $path = mysqli_real_escape_string($con, $path);
 $query="insert into volleyupload(photo,name)values('".$path."','".$name."');";
$result=mysqli_query($con,$query);

if($result)
 {
 echo "success";

 }
 else{

 echo "failure";
 }

  mysqli_close($con);
  }
 }
 ?>
Aparna
  • 83
  • 1
  • 1
  • 8
  • Because you don't write the binary data in the database? You're only inserting the path + name. Or you mean, the path+name aren't inserted? – Ali Alwash Nov 04 '16 at 10:01
  • In my database I've made fields like photo and name in datatype varchar.I want to store path to photo and name of image to name field.But this is failing – Aparna Nov 04 '16 at 10:05
  • See this http://stackoverflow.com/q/11929145/3885509 – Charlotte Dunois Nov 04 '16 at 10:09
  • I just want to insert the path and name..But it's not working..Is there any error in my php script..I got path when toasted in app.But insertion not occuring – Aparna Nov 04 '16 at 10:14
  • thank you all for helping me..I made a new table in database and all went fine.. – Aparna Nov 04 '16 at 10:20
  • For the next time, post the mysqli_error($link) if the insert failed. Probably that answers your question even before asking it here ;) – Ali Alwash Nov 04 '16 at 10:27

0 Answers0