0

I am trying to upload a file from the local Drawable folder and I am getting errors. I tried several code snippets that I found on the internet (some from stackoverflow), but nothing seems to work. My problem seems to be the instantiation of a File object so that I can create a FileBody from it, which in turn gets added to the MultipartEntityBuilder via the addPart method. Everything looks OK and compiles, but when the HttpClient executes the post at runtime, I get an error that says the filename is too long.

Here is the code snippet. I have this inside a doInBackground event of an AsyncTask:

HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(HTTP.USER_AGENT, global.GeUserAgent());
HttpPost httppost = new HttpPost(***URLHERE***);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
InputStream ins = getResources().openRawResource(R.drawable.IMAGE_I_AM_TRYING_TO_UPLOAD_HERE);
BufferedReader br = new BufferedReader(new InputStreamReader(ins));
StringBuffer sb = new StringBuffer();
String line;
while((line = br.readLine()) != null){
sb.append(line);
}
file1 = new File(sb.toString());
FileBody fileBody = new FileBody(file1);
builder.addPart("file1",fileBody);
httppost.setEntity(builder.build());
HttpResponse response = httpclient.execute(httppost);

Does anyone have any ideas? Thank you in advance.

Valtinho
  • 327
  • 1
  • 3
  • 16

1 Answers1

0

Ok, I just found the answer myself right here on stackoverflow. The answer lies in using addBinaryBody instead of addPart of the MultipartEntityBuilder:

Android create file from drawable

I hope this helps someone.

Community
  • 1
  • 1
Valtinho
  • 327
  • 1
  • 3
  • 16