gpt4 book ai didi

android - 从android发布文件时的内存问题

转载 作者:太空宇宙 更新时间:2023-11-03 12:41:25 26 4
gpt4 key购买 nike

当我尝试从我的 Android 应用程序上传图像或更大的文件时,它崩溃并出现 OutOfMemoryException。我想知道是否有其他方法可以做到这一点。

我在两个不同的地方遇到了应用程序崩溃:

Base64.encodeToString(bytes, Base64.DEFAULT);

这里的 base64 字符串是 nameValuPairs 集合中的值之一。

HttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost(uo.WebServiceURL);
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nameValuePairs); // On this line
httppost.setEntity(entity);
HttpResponse response = client.execute(httppost);

有什么想法吗?

最佳答案

If you do it like that, the whole POST must be buffered in memory. This is because it needs to send the Content-Length header first.

Instead, I think you want to use the Apache HTTP libraries, including http://developer.android.com/reference/org/apache/http/entity/FileEntity.html . That will let it figure out the length before reading the file. You can use this answer as a starting point. But the second parameter to the FileEntity constructor should be a mime type (like image/png, text/html, etc.).

Posting a large file in Android

检查这也.........

正如 Schnapple 所说,您的问题似乎非常宽泛,难以阅读和理解。

下面是发送 HTTP POST 并从服务器获取响应的一些通用代码,尽管这可能会有帮助。

public String postPage(String url, File data, boolean returnAddr) {

ret = null;

httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);

httpPost = new HttpPost(url);
response = null;

FileEntity tmp = null;

tmp = new FileEntity(data,"UTF-8");

httpPost.setEntity(tmp);

try {
response = httpClient.execute(httpPost,localContext);
} catch (ClientProtocolException e) {
System.out.println("HTTPHelp : ClientProtocolException : "+e);
} catch (IOException e) {
System.out.println("HTTPHelp : IOException : "+e);
}
ret = response.getStatusLine().toString();

return ret;
}

关于android - 从android发布文件时的内存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7319444/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com