surely there are many post of these for several places, I have read several and I have looked for several examples. Not being an expert on Android there are some things that I can not understand, so I do this POST, because the weird thing is that this method I use worked well. I know that HttpPost and HttpClient are already deprecated but I have them in several places in my application and it's still working.
I put the code on both the Android side and the PHP side
public void subirFoto(String foto){
String miFoto = foto;
try {
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost(servidor_fotos);
File file = new File(miFoto);
MultipartEntity mpEntity = new MultipartEntity();
ContentBody foto3 = new FileBody(file, "image/jpeg");
mpEntity.addPart("fotoUp", foto3);
httppost.setEntity(mpEntity);
httpclient.execute(httppost);
httpclient.getConnectionManager().shutdown();
Log.e("sistema","FOTO SUBIDA");
} catch (Exception e) {
Log.e("sistema","ERROR AL SUBIR LA FOTO");
e.printStackTrace();
}
}
This code what it does is take the images from a directory of the device and send it to a url. The url I corroborated it and it's the correct one, I also corroborate finding the image on the device.
The problem is that does not save it on the server .
ERROR does not throw it in this code, on the contrary it says "photo upload" . The problem is that it is not coming to PHP, the APP when it is in this process takes a prudent time like you are sending the photo, but in PHP I pull upload error and I really do not understand why that .. because it was working perfect
I leave the PHP code
<?php
if ($_FILES["fotoUp"]["error"] > 0) {
$error = "Error en el archivo: " . $_FILES["fotoUp"]["error"] . "<br />";
}
$foto = $_FILES["fotoUp"]["name"];
$total = strlen ($foto);
$file=fopen("datos.txt","w");
fputs($file,"FOTO: " . $foto . PHP_EOL);
fputs($file,"Error: ". $error . PHP_EOL);
$ruta = "fotos/" . basename( $_FILES["fotoUp"]["name"]);
try{
if(move_uploaded_file($_FILES["fotoUp"]["tmp_name"], $ruta)){
fputs($file,"Fotos Guardada correctamente" .PHP_EOL);
chmod ("fotos/" .basename( $_FILES["fotoUp"]["name"]), 0644);
}else{
fputs($file,"Imagen: " . $_FILES["fotoUp"]["tmp_name"] .PHP_EOL);
fputs($file,"Ruta: " . $ruta .PHP_EOL);
fputs($file,"Error al guardar la foto" .PHP_EOL);
}
} catch (Exception $e) {
$error2 = 'Error: ' . $e->getMessage();
}
fputs($file,"Error2: ". $error2 . PHP_EOL);
fclose($file);
?>
There are a few more lines because I keep the errors in a TXT and what I get with this code is the following txt
PHOTO: Error: Image: Route: photos / Error saving the photo Error2:
** That is to say that the photo is never coming and when you try to save it it throws the error ***
Now my question is why the photo does not arrive? I reiterate the URL is fine because in addition the PHP file is executed therefore it is arriving correctly but as the photo does not arrive
THANKS