简体   繁体   English

使用位图Android的outofmemory错误

[英]outofmemory error using bitmaps Android

I've created an android app, which chooses a picture from Gallery and displays a preview. 我创建了一个Android应用程序,它从Gallery中选择一张图片并显示预览。

@Override
public void onClick(View v) {
    if (v.getId()== R.id.button){

        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_PICK);
        startActivityForResult(Intent.createChooser(intent,
                "Select Picture"), SELECT_PICTURE);
    }

After the image is selected, the preview should be shown. 选择图像后,应显示预览。

Yet, it works only for the first time. 然而,它只是第一次有效。 And later when I click back, it shows outOfMemoryException 后来当我点击它时,它显示outOfMemoryException

working with bitmaps in android costs you a lot of memory, which needs a hude attention because of memory leaks. 在android中使用bitmaps会花费你很多内存,因为内存泄漏需要引起人们的注意。

you can always use 你可以随时使用

System.gc()

to garbage collect and free up some memory. 垃圾收集和释放一些内存。

or 要么

bitmap.recycle();

cheack out these blog post that I used when I developed my image editing app. 在我开发我的图像编辑应用程序时,我用这些博客文章。

Working with bitmaps in android often throws the OutOfMemory error. 在android中使用位图通常会抛出OutOfMemory错误。 Bitmaps need to be handled properly. 位图需要正确处理。 you might want to look at the following libraries used specifically for image loading and working with bitmaps in android: 您可能希望查看以下专门用于图像加载和在android中使用位图的库:

https://github.com/nostra13/Android-Universal-Image-Loader https://github.com/nostra13/Android-Universal-Image-Loader

https://github.com/novoda/ImageLoader https://github.com/novoda/ImageLoader

You can also implement your own imageloader. 您还可以实现自己的图像加载器。 You can find the code for that easily. 您可以轻松找到相应的代码。

you are probably caching a lot of bitmaps, so you could use ImageLoader and do something like this: 你可能正在缓存很多位图,所以你可以使用ImageLoader并做这样的事情:

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                this).memoryCache(new WeakMemoryCache())
                .discCache(new UnlimitedDiscCache(new File("/cache"))).build();

also try somehow to release bitmaps after you no longer need them 在你不再需要它们之后,还会以某种方式尝试释放位图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM