Reputation: 2355
I am trying to build an app with Codename One that uploads to a server a photo captured from the camera and then shrunk to 300x300 along with GPS coordinates.
On the simulator (Iphone or ANdroid) everything works well, the photo is received and stored on the server, the other data as well. After building the Android app it also works fine on Android.
However when it comes to building the iOS app and testing it on my Iphone 4, the photo is never received. Indeed the data does not pass the validator in Laravel :
$validator = Validator::make($submittedData->all(), [
'longitude' => 'required|numeric',
'latitude' => 'required|numeric',
'accuracy' => 'required|numeric',
'pic' => 'required|image|mimes:jpeg|max:100',
]);
Usually the size of the received file is around 10k but here it is less than 1k as reported from laravel's logs :
[2016-05-18 15:27:10] local.INFO: POST /public/www/API/storeAPI HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: fr-fr
Connection: close
Content-Length: 518
Content-Type: multipart/form-data; boundary=154c47a4886
Cookie: cluster=R2881455720
Host: lambda.fr
Remote-Ip: x.y.z.a
User-Agent: MyApp/2.8 CFNetwork/672.1.15 Darwin/14.0.0
X-Predictor: 1
So I'm not sure if I did something wrong in Codename One or if it has something to do with Laravel.
In CN1 I used the following to send data :
`MultipartRequest requete = new MultipartRequest();
requete.setUrl(URL_DEST);
requete.setTimeout(ParametresGeneraux.REQUEST_TIMEOUT);
requete.setPost(true);
requete.setFailSilently(true);
requete.setSilentRetryCount(ParametresGeneraux.SEND_NUMBER_OF_RETRY);
requete.addData("pic", storageDir + imgPath, "image/jpeg");
requete.addArgument("latitude", Double.toString(location.getLatitude()));
requete.addArgument("longitude", Double.toString(location.getLongitude()));
requete.addArgument("accuracy", Double.toString(location.getAccuracy()));
NetworkManager.getInstance().addToQueueAndWait(requete);`
Any help would be appreciated to make the pic upload also works on Iphone device !
EDIT 1: It appears that the resized image that I send ("storageDir + imgPath" above) has a length of 0B on my Iphone (although it works on Android and on the simulator). So I will have a closer look at the resizing method which is launched from the StateMachine and looks like below.
EDIT 2: If I add the line that gives the file length, I get a value in the simulator but 0 on the real Iphone 4 device. Consequently my initial problem has nothing to do with uploading a file but with path. Should I open a new question ?
`public static final boolean resizeImageFile (Image image, String outpath, String outFormat, final int width, final int height) {
Image scaledImage = image.scaled(width, height);
// Par défaut on prend le format jpeg
if(!(outFormat.equals(ImageIO.FORMAT_JPEG) || outFormat.equals(ImageIO.FORMAT_PNG)) ){
outFormat = ImageIO.FORMAT_JPEG;
}
try {
OutputStream os = Storage.getInstance().createOutputStream(outpath);
if ( scaledImage != null ) { /*
* ATTENTION le paramètre de qualité ne semble pas être pris en compte
*/
ImageIO.getImageIO().save(scaledImage, os, outFormat, ParametresGeneraux.getPicQuality());
os.close();
/*ADDED LINE FOR EDIT 2 */ Dialog.show("Size of saved file", FileSystemStorage.getInstance().getLength(HOME_DIR + filename) + "B", "OK", null); // this shows xxxB on Simulator and 0B on device (Iphone 4)
return true;
}
else {
return false;
}
} catch (IOException e) {
return false;
}
}`
EDIT 3 : Finally the problem has been solved in the following way. Instead of using Storage.getInstance().createOutputStream(outpath); which stores the file (here outputpath) somewhere in the Storage, I switched to OutputStream os = FileSystemStorage.getInstance().openOutputStream(outpath); As FileSystemStorage only deals with absolute path, if outpath = FileSystemStorage.getInstance().getAppHomePath + aFileNameOfYourChoice then you can send data with requete.addData("pic", FileSystemStorage.getInstance().getAppHomePath + aFileNameOfYourChoice, "image/jpeg");
Regards
Upvotes: 3
Views: 148
Reputation: 52760
You need to always use FileSystemStorage
and always use an absolute path to a file in order to upload it. Storage
is simpler and abstracted from native OS's thus it means different things on different OS's.
Upvotes: 1