1

我需要从相机中以最大分辨率(例如 12mpx)发布大图像。但是当我解码文件流以获取要发布的 byteArrayInputStream 时,我经常会遇到 OutOfMemoryError。有没有其他方法可以张贴大图?

Ps 我不需要显示或缩放这张照片。

4

3 回答 3

2

是的,您可以通过 MultipartEntity 发布图像/文件,请在下面找到示例片段:

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

要使用多部分实体,您需要下载 httpmime-4.1.2.jar 并将其添加到项目的构建路径中。

于 2012-12-08T09:16:24.067 回答
2

android:largeHeap="true"如果您使用的API级别大于或等于,请尝试在应用程序级别的清单中使用此行 11

于 2012-12-08T09:17:44.427 回答
0

如果可以以原始图像格式发布,则直接从文件流发送数据:

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

复制流功能:

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);
}
于 2012-12-08T10:43:50.390 回答