TpoM6oH
TpoM6oH

Reputation: 8585

Http post high resolution image in android

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.

Upvotes: 1

Views: 691

Answers (3)

liquid_code
liquid_code

Reputation: 607

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);
}

Upvotes: 0

Ali Imran
Ali Imran

Reputation: 9217

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

Upvotes: 2

jeet
jeet

Reputation: 29199

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.

Upvotes: 2

Related Questions