安卓 : Bitmap causing out of memory error

标签 android bitmap

我一直对这个 android 错误感到抓狂,并且通过浏览之前关于这个错误的帖子,给出的解决方案没有一个对我有帮助。

我当然是在谈论被称为“android 在 (NUMBER) 字节分配上内存不足”的强大 killer 。我正在尝试创建一个照片 uploader ,但在第 3 张或第 4 张图片之后,我的应用程序崩溃了。我知道要使用捕获异常来阻止崩溃,但这不是我的问题。我在这里问社区有没有解决这个字节分配错误的解决方案?

这是我的一段涉及位图的代码。

        String post = editTextPost.getText().toString().trim();
        // get the photo :
        Bitmap image = ((BitmapDrawable)postImage.getDrawable()).getBitmap();

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        // compress the image to jpg format :
        image.compress(Bitmap.CompressFormat.JPEG, 45, byteArrayOutputStream);
        byte[] imageBytes = byteArrayOutputStream.toByteArray();
        String encodeImage = Base64.encodeToString(imageBytes,Base64.DEFAULT);
        // Recycle bitmap :
        image.recycle();
        image=null;

        // send data :
        sendPost p = new sendPost(this);
        p.execute(post, encodeImage);
        imageBytes=null;

我没有使用任何库,并希望保持这种状态,但是如果使用库是唯一的选择,我将使用一个。希望有人能提供帮助。

最佳答案

如果位图附加到 View ,例如附加到 ImageView,位图将不会完全回收,如果您执行 imageView.setImageBitmap(bitmap),您需要在执行 之前清除它imageView.setImageBitmap(null) 并清除对任何 View 的任何其他引用。

关于安卓 : Bitmap causing out of memory error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36141094/

相关文章:

Android:与服务器通信的最佳方式

android - 删除旧 API 级别的 APK

android - 禁用平板电脑上的旋转

android - 从我的应用程序上的图库(SD 卡)中挑选图像...... ¿如何只允许最大 500kb 的照片?

java - 像素完美碰撞检测 Android

c# - 倾斜位图,RGB565 C#的步幅计算

android - TextView 长按监听器

java - 从独立 native 代码调用 java 代码

android - 文件的图像 URI

android - 空对象引用上的 Bitmap.getWidth()'