【问题标题】:how do you recycle the bitmap loaded from a file into an ImageView when trying to load another into it尝试将另一个文件加载到 ImageView 时,如何将从文件加载的位图回收到 ImageView 中
【发布时间】:2015-01-06 08:36:39
【问题描述】:
File imgFile = new  File("/sdcard/Images/test_image.jpg");

if(imgFile.exists()){

    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);

    myImage.setImageBitmap(myBitmap);

}

//这一切都很好,直到再次调用 myImage.setImageBitmap (在拍摄照片并加载到这个小的“后视图”ImageView 矩形即 myImage 之后)并且内存不足。

【问题讨论】:

  • 您是否尝试过禁用图像缓存,或者在图像视图上禁用 invalidate()?
  • 您可以创建自定义类并在 ImageView 上的图像更改时通知,然后回收位图。从这里有一些想法stackoverflow.com/questions/13622081/…

标签: android bitmap recycle


【解决方案1】:
Bitmap myBitmap;
if(imgFile.exists()){

     if(myBitmap!=null){
         myBitmap.recycle();
         myBitmap=null;
     }
     myBitmap= BitmapFactory.decodeFile(imgFile.getAbsolutePath());

    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);

    myImage.setImageBitmap(myBitmap);

}

【讨论】:

  • 我应该注意防止内存不足错误的重要性,我需要考虑我的图像来源(相机)。我的图像太大了,特别是考虑到我在计时器上有另一个处理程序,它同时通过 http 加载和发送图片(当连接可用时)。当我设置我的相机时,我必须设置一些参数:c = Camera.open(); Camera.Parameters p = c.getParameters(); p.setJpegQuality (75); p.setPictureSize (640,480); c.setParameters(p);
  • @murphy 你想知道什么。你能澄清一下吗?
【解决方案2】:

我认为这会对你有所帮助。

File imgFile = new  File("/sdcard/Images/test_image.jpg");
private Bitmap mLastUpdatedBitmap;

if(imgFile.exists()){
    if(mLastUpdatedBitmap != null && !mLastUpdatedBitmap.isRecycled()){
       mLastUpdatedBitmap.recycle();
    }
    mLastUpdatedBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
    myImage.setImageBitmap(mLastUpdatedBitmap);

}

【讨论】:

    猜你喜欢
    • 2020-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-15
    • 1970-01-01
    • 2015-10-30
    相关资源
    最近更新 更多