【问题标题】:Lack of memory with multiple images [closed]多张图像缺乏内存[关闭]
【发布时间】:2013-09-13 23:03:52
【问题描述】:

在我的应用程序中,我必须管理由 Android 相机拍摄的大量图像。

问题是当我有很多图片时,手机内存不足并且运行速度很慢。 我希望仍然能够管理相同数量的图像,但没有内存问题

关于我应该做些什么来实现这一点有什么建议吗?

【问题讨论】:

  • 发布代码时内存不足的地方
  • 管理得更好?没有代码,我们无能为力...
  • 这个问题如何获得 5 票?难以置信!
  • 问题被问得很糟糕,也许不是为了适度关注,但在我看来不应该被赞成

标签: java android image android-camera


【解决方案1】:

您应该解码缩放的图像。
您可以通过将 JPEG 扩展为已缩放以匹配目标视图大小的内存数组来减少使用的动态堆的数量。
以下示例方法演示了这种技术:

private void setPic() {
    // Get the dimensions of the View
    int targetW = mImageView.getWidth();
    int targetH = mImageView.getHeight();

    // Get the dimensions of the bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    // Determine how much to scale down the image
    int scaleFactor = Math.min(photoW/targetW, photoH/targetH);

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;

    Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
    mImageView.setImageBitmap(bitmap);
}

阅读更多here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多