从 Android 发布文件时出现内存问题

发布于 2024-12-03 07:25:54 字数 536 浏览 1 评论 0原文

当我尝试从 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);

有什么想法吗?

When I try to upload images or larger files from my android app it crashes with an OutOfMemoryException. Im wondering if there are any alternate ways of doing this.

Ive had the application crash in two different spots:

Base64.encodeToString(bytes, Base64.DEFAULT);

And here where the base64 string is one of the values in the nameValuPairs collection.

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

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北城挽邺 2024-12-10 07:25:54

如果你这样做,整个 POST 必须缓冲在内存中。
这是因为它需要先发送 Content-Length 标头。

相反,我认为您想使用 Apache HTTP 库,包括
http://developer.android.com/reference/org/ apache/http/entity/FileEntity.html
。这将让它在读取文件之前计算出长度。你
可以使用这个答案作为起点。但第二个参数为
FileEntity 构造函数应该是 mime 类型(如 image/png,
文本/html等)。

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

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

Check This also .........

As Schnapple says your question seems very broad and is confusing to read and understand.

Here is some general code to send a HTTP POST and get a response from a server though that may be helpful.

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;
}
森罗 2024-12-10 07:25:54

这是因为图像尺寸太大,有一些关于如何解决此问题的解释,我将向您指出一个已经提出的问题。

将图像加载到位图对象时出现奇怪的内存不足问题

希望这有帮助。

It is because the image size is too high, there is some explanation in to how to resolve this issue, i will point you to a question that has already been asked.

Strange out of memory issue while loading an image to a Bitmap object

hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文