简体   繁体   English

如何在ArrayList中存储大型位图

[英]how can i store store large Bitmaps in a ArrayList

I am using below code this way but when I add images into ArrayList after sometime my app crashes and it goes out of memory error 我在这种方式下使用下面的代码,但是当我将图像添加到ArrayList后,我的应用程序崩溃并且内存不足错误

                    ArrayList<Bitmap> rev = new ArrayList<Bitmap>(10000);
                    for (long i = 1000000; i < millis * 1000; i += 1000000) 
                    {

                            finalBitmap = retriever.getFrameAtTime(i,MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
                            rev.add(finalBitmap);
                            try
                            {
                                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                                finalBitmap.compress(Bitmap.CompressFormat.PNG, 40, bytes);
                                File f = new File(saveFolder, ("img0"+j+".png"));
                                f.createNewFile();
                                FileOutputStream fo = new FileOutputStream(f);
                                fo.write(bytes.toByteArray());
                                fo.flush();
                                fo.close();
                                j++;
                            }
                             catch (IOException e) 
                             {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                             }  
                        }

With the limited memory available you will not be able to have a direct reference to each of the images, otherwise like you say you will run out of memory. 由于内存有限,您将无法直接引用每个图像,否则就像您说的那样,内存不足。 These images will take up space in memory as long as there is a reference to your BitMap objects. 只要有对BitMap对象的引用,这些图像就会占用内存空间。 To free up this memory you need to dereference these BitMaps and or clear the ArrayList, this will allow the Garbage Collector to free up the unused memory. 要释放此内存,您需要取消引用这些BitMap或清除ArrayList,这将使垃圾回收器释放未使用的内存。 Ideally you only want a reference only to the images you can see, or the ones you will see next. 理想情况下,您只希望仅参考您可以看到的图像,或者仅是接下来要看到的图像。

You could just look up the 8 images for every page view, or perhaps the the images in the surrounding page views. 您可以只为每个页面视图查找8个图像,或者查看周围页面视图中的图像。 If the images you are display are much larger than the size they take up then you may want to resize these images on the fly and only store them as the size they appear, this should also help reduce the memory footprint of your images. 如果您显示的图像比它们占用的图像大得多,那么您可能希望动态调整这些图像的大小并仅将它们存储为它们出现的大小,这也应该有助于减少图像的内存占用。

The Android developers have written a series of tutorials on just this topic: http://developer.android.com/training/displaying-bitmaps/index.html Android开发人员就此主题编写了一系列教程: http : //developer.android.com/training/displaying-bitmaps/index.html

If you need to pass an stay of images to an activity then you ate better off sending an stay of file paths to it if you can change the activity. 如果您需要将图像的暂留传递给活动,那么最好更改发送的文件路径的暂存,如果可以的话。

Otherwise you will need to new to read the bitmaps with a later sample size so they are smaller but consume less memory. 否则,您将需要重新读取具有更高样本大小的位图,以使它们变小但占用更少的内存。

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html#load-bitmap http://developer.android.com/training/displaying-bitmaps/load-bitmap.html#load-bitmap

You can use 4*width*height to determine the size of the bitmap when it's in memory and use that to determine how much memory you will allocate. 您可以使用4 * width * height来确定位图在内存中的大小,并使用它来确定要分配多少内存。

Use BitmapFactory.Option.inJustDecodeBounds to get the size of the bitmap without loading it 使用BitmapFactory.Option.inJustDecodeBounds来获取位图的大小而不加载它

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

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