我正在尝试使用下面的代码让我的应用程序将图像上传到网络服务器。它有时会起作用,但似乎也会因内存错误而失败。如果文件太大,有人可以发布一个如何完成此操作的示例吗?此外,我正在构建它以支持 1.5 及更高版本。我不介意提供给我的代码是否会在上传前将图像尺寸调整得更小。
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();
最佳答案
您有两个选项可以使您的代码可用。
您应该使用多部分方法上传更大的文件。我在我的代码中使用的。 Its Apache code . (是的,您可以轻松地将其移植到您的 Android 项目中)。
您可以最小化图像分辨率。通过使用 SampleSize 标志, 关注this链接。
希望对你有帮助。
关于Android - 将大图像上传到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4941580/