Upload image to server from android studio with WebService

1

I'm starting on android and I still can not react very well to the errors, I'm working on an application to capture images from the camera and upload them to the server everything works perfect but at the time of pressing the button upload only stores the name of the image in my table and does not upload the image to the folder

the php files are

upload.php

    <?PHP
$ruta = "imagenes/" .basename($_FILES['fotoUp']['name']);
if(move_uploaded_file($_FILES['fotoUp']['tmp_name'], $ruta))
       chmod ("uploads/".basename( $_FILES['fotoUp']['name']), 0644);
?>

insertImage.php

    <?PHP
$hostname_localhost ="localhost";
$database_localhost ="basededatos";
$username_localhost ="root";
$password_localhost ="";
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or
trigger_error(mysql_error(),E_USER_ERROR); 

mysql_select_db($database_localhost, $localhost);

$imagen=$_POST['imagen'];

$query_search = "insert into imagenes(imagen) values ('".$imagen."')";
$query_exec = mysql_query($query_search) or die(mysql_error());

mysql_close($localhost);
?>

the android method

    private void uploadFoto(String imag) {
        HttpClient httpclient = new DefaultHttpClient();
        httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
        HttpPost httppost = new HttpPost("/upload.php");
        MultipartEntity mpEntity = new MultipartEntity();
        ContentBody foto = new FileBody(file, "image/jpeg");
        mpEntity.addPart("fotoUp", foto);
        httppost.setEntity(mpEntity);
        try {
            httpclient.execute(httppost);
            httpclient.getConnectionManager().shutdown();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 private boolean onInsert(){
        HttpClient httpclient;
        List<NameValuePair> nameValuePairs;
        HttpPost httppost;
        httpclient=new DefaultHttpClient();
        httppost= new HttpPost("/insertImagen.php");
        nameValuePairs = new ArrayList<NameValuePair>(1);
        boolean imagen = nameValuePairs.add(new BasicNameValuePair("imagen", nombreImagen.getText().toString().trim() + ".jpg"));

        try {
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            httpclient.execute(httppost);
            return true;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
    
asked by 06.07.2016 в 16:58
source

2 answers

1

But the server should do both operations in a single class, so that Android connects to a single route. I leave my code

Create a file called fileupload.php

<?php

    if ($_FILES["file"]["error"] > 0) {
        echo "Error en el archivo: " . $_FILES["file"]["error"] . "<br />";
    } else {
        $target_path = "uploads/" . basename($_FILES['file']['name']);  // Ubicación para dejar los archivos subidos
        try {
            /*//Para no sobreescribir
            if (file_exists("upload/" . $_FILES["file"]["name"])){
                echo " El archivo " . $_FILES["file"]["name"] . " ya existe";
            }else{*/
            if (!move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
                throw new Exception('El archivo es demaciado grande');
            }
            echo "El archivo " . basename($_FILES['file']['name']) . " ha sido subido correctamente" . "\n";
        } catch (Exception $e) {
            echo 'Error: ' . $e->getMessage();
        }
    }

?>

then you create a folder called "upload" in the same directory.

To develop you can implement a php project in link

and then point your android activity to the server:

HttpPost httppost = new HttpPost("https://tucuenta.c9users.io/fileUpload.php");

and change the parameter you send to "file" so that it looks like the server

mpEntity.addPart("file", foto);
    
answered by 02.02.2017 в 20:41
0

In HttpPost httppost = new HttpPost("/upload.php"); you must put the url of the server to which you expect to send the image, it should be something like this: HttpPost httppost = new HttpPost("http://localhost:8080//upload.php"); I hope I have helped you

    
answered by 02.02.2017 в 20:04