1

I need to post a large image from camera with max resolution (for example 12mpx). But I often get OutOfMemoryError when I decode filestream to get a byteArrayInputStream to post. Is there any other way of posting large images?

P.s. I don't need to display or scale this photo.

3 Answers 3

2

Yes, you can post images/files by MultipartEntity, please find sample snippet, below:

HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); MultipartEntity entity = new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE); File file= new File(filePath); if(file.exists()) { entity.addPart("data", new FileBody(file)); } httppost.setEntity(entity); HttpResponse response = httpclient.execute(httppost); 

To use multipart entity you would need to download and add httpmime-4.1.2.jar to the build path of the project.

Sign up to request clarification or add additional context in comments.

Comments

2

Try using this line in manifest at application level android:largeHeap="true" if you are using API level greater or equal to 11

Comments

0

If it's possible to post in original image format then send data directly from file stream:

FileInputStream imageIputStream = new FileInputStream(image_file); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); OutputStream out = connection.getOutputStream(); copyStream(imageIputStream, out); out.close(); imageIputStream.close(); 

copyStream function:

static int copyStream(InputStream src, OutputStream dst) throws IOException { int read = 0; int read_total = 0; byte[] buf = new byte[1024 * 2]; while ((read = src.read(buf)) != -1) { read_total += read; dst.write(buf, 0, read); } return (read_total); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.