【发布时间】:2012-12-08 09:12:41
【问题描述】:
我需要从相机中以最大分辨率(例如 12mpx)发布大图像。但是当我解码文件流以获取要发布的 byteArrayInputStream 时,我经常会遇到 OutOfMemoryError。有没有其他发大图的方法?
附:我不需要显示或缩放这张照片。
【问题讨论】:
标签: android bitmap stream http-post
我需要从相机中以最大分辨率(例如 12mpx)发布大图像。但是当我解码文件流以获取要发布的 byteArrayInputStream 时,我经常会遇到 OutOfMemoryError。有没有其他发大图的方法?
附:我不需要显示或缩放这张照片。
【问题讨论】:
标签: android bitmap stream http-post
是的,您可以通过 MultipartEntity 发布图像/文件,请在下面找到示例 sn-p:
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 并将其添加到项目的构建路径中。
【讨论】:
如果您使用的API 级别大于或等于11,请尝试在应用程序级别android:largeHeap="true" 的清单中使用此行
【讨论】:
如果可以以原始图像格式发布,则直接从文件流中发送数据:
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 函数:
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);
}
【讨论】: