I'm trying to upload some photos taken by an application in a distant database. To be clear :
- I take a piture from my app
- I send if to a web service (witten in .net, svc file)
- The WS upload it in the database
The way i'm doing it : i'm sending the byte[] recovered from the picture that way
String url = MyAddress + "insert_img?login='"
+ login_result + "'&epc='" + code + "'&title='" + t
+ "'&image='" + base64EncodedString + "'&descrip='" + d
+ "'";
BufferedReader inStream = null;
System.out.println(url);
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpR = new HttpGet(url);
httpR.setHeader("Accept", "application/json");
httpR.setHeader("Content-type", "application/json");
HttpResponse response = httpClient.execute(httpR);
My problem is that I have to compress A LOT the picture in order it to be send. The picture, or base64EncodedString field, is created like that :
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
mImageView.setImageDrawable(null);
mImageView.setImageBitmap(thumbnail);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 1, baos);
byte[] outputByteArray = baos.toByteArray();
base64EncodedString = Base64.encodeToString(outputByteArray, Base64.NO_WRAP + Base64.URL_SAFE);
As you can see, the image is very very very compressed, and when I recover it it's more or less a 4-colors stamp. And if I do not compress it a lot, image is not inserted. So first of all : Am I doing it the right way to deal ? And also, why is there no insertion when the picture is too big and how, if possible, to track the error ?
Thanks !
EDIT :
I've modified my code that way, server side :
public string insert_report(Stream stream)
{
string login = "";
string epc = "";
string title = "";
string image = "";
string descrip = "";
MultipartParser parser = new MultipartParser(stream);
Entities entities = new Entities();
string res = "";
if (parser != null && parser.Success)
{
res += parser.Filename;
foreach (var content in parser.MyContents)
{
string name = content.PropertyName;
string str = Encoding.UTF8.GetString(content.Data);
res += "name : " + name +"---";
res += "content : " + str+"---";
if (name.Contains("login"))
{
login = str;
}
if (name.Contains("epc"))
{
epc = str;
}
if (name.Contains("title"))
{
title = str;
}
if (name.Contains("image"))
{
image = str;
}
if (name.Contains("descrip"))
{
descrip = str;
}
}
}
res+= " res returned by server : "+ entities.insert_report(login, epc, title, image, descrip);
entities.SaveChanges();
return res;
}
But there are some problems : the insert_report function return -1... How do I recover the error given by that function ?
Do you receive 414 (Request-URI Too Long) error? You should use HTTP POST method to upload your images as POST body - no Base64 encoding will be also required. Look at https://stackoverflow.com/a/7632849/2714032