Queria consultarles sobre los metodos usados para subir imagenes a un servidor.
Es mejor el metodo POST o con PHP?
Yo he usado lo siguiente, y todavia no logro hacerlo funcionar...alguno lo ha implementado de otra manera?
Código:
try { HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); // here, change it to your php; HttpPost httpPost = new HttpPost("http://www.xxxxxxxxxxxxxxx.com.ar/archivos/ImageUtil.php"); MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); bitmap = BitmapFactory.decodeFile("test111.jpg"); Bitmap bmpCompressed = Bitmap.createScaledBitmap(bitmap, 640, 480, true); ByteArrayOutputStream bos = new ByteArrayOutputStream(); bmpCompressed.compress(CompressFormat.JPEG, 100, bos); byte[] data = bos.toByteArray(); // sending a String param; entity.addPart("myParam", new StringBody("my value")); // sending a Image; // note here, that you can send more than one image, just add another param, same rule to the String; entity.addPart("myImage", new ByteArrayBody(data, "temp.jpg")); httpPost.setEntity(entity); HttpResponse response = httpClient.execute(httpPost, localContext); BufferedReader reader = new BufferedReader(new InputStreamReader( response.getEntity().getContent(), "UTF-8")); String sResponse = reader.readLine(); } catch (Exception e) { Log.v("myApp", "Some error came up"); }
Código PHP:
Ver original
<?php class ImageUtil{ const uploadDir = "img"; const allowedExtensions = "jpg, jpeg, gif, png"; const maxSize = 2; //MB const maxWidth = 1024; const maxHeight = 1024; private static function validExtension($extension){ } private static function validSize($size){ } private static function validDimensions($width, $height){ return $width<ImageUtil::maxWidth && $height < ImageUtil::maxHeight; } private static function uploadedFile($file, $newFile){ } public static function uploadImage($file, $newName){ $extension = $extension[extension]; // check extension if (!ImageUtil::validExtension($extension)) throw new InvalidExtensionException(); // check dimensions if (!ImageUtil::validDimensions($width, $height)) throw new InvalidDimensionsException(); // check size if (!ImageUtil::validSize($file['size'])) throw new InvalidSizeException(); // move to needed location if (!ImageUtil::uploadedFile($file['tmp_name'], $newName.".".$extension)) throw new UnableToUploadException(); return true; } } class InvalidExtensionException extends Exception { protected $message = 'Extension is invalid'; // exception message } class InvalidDimensionsException extends Exception { protected $message = 'Image dimensions are invalid'; // exception message } class InvalidSizeException extends Exception { protected $message = 'Size is invalid'; // exception message } class UnableToUploadException extends Exception { protected $message = 'Unable to upload file'; // exception message } ?>
Muchas gracias!!