Reputation: 21
I am trying to have my application upload images to the webserver using the code below. It works sometimes, but also seems to fail with a memory error. Can someone please post an example of how to accomplish this if the file size is to big? Also, I am building this to support 1.5 and up. I don't mind if the code given to me resizes the image smaller before upload.
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(urlString);
File file = new File(nameOfFile);
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamEntity reqEntity = new InputStreamEntity(fileInputStream, file.length());
httppost.setEntity(reqEntity);
reqEntity.setContentType("binary/octet-stream");
HttpResponse response = httpclient.execute(httppost);
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
responseEntity.consumeContent();
}
httpclient.getConnectionManager().shutdown();
Upvotes: 2
Views: 7497
Reputation: 15798
You can try following code for uploading images.
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
MultipartEntity multiPart = new MultipartEntity();
multiPart.addPart("my_picture", new FileBody(new File(IMG_URL)));
httpPost.setEntity(multiPart);
HttpResponse res = httpClient.execute(httpPost);
Upvotes: 1
Reputation: 536
you should really look into setChunkedStreamingMode of the connection. or, indeed, use MultipartEntity (it's apache httpcomponents library - you will easily find lots of solutions on this.
as for resizing images, it's quite simple:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2; // look at the link Rajnikant gave for more details on this
Bitmap bitmap = BitmapFactory.decodeFile(filename, options);
// here you save your bitmap to whatever you want
but it is memory consuming too...
Upvotes: 0
Reputation: 1097
You have two option to make your code workable.
You should use multi part approach to upload larger size file. Which i am using in my code. Its Apache code. (Yes, you can easily port it in your Android project).
You can minimize the image resolution. by using SampleSize flag, Follow this link.
I hope it help.
Upvotes: 1