应用程序崩溃在for循环中使用Bitmap

时间:2015-01-09 11:20:49

标签: android bitmap

for (int k = 0; k < file.length; k++) {
        Bitmap myBitmap= null;
        myBitmap = BitmapFactory.decodeFile(file[k].getAbsolutePath());
        bitMap.add(myBitmap);

}

当我使用此代码存储所有图像时,我的应用程序崩溃了  位图Arraylist,对于较少数量的图像,它工作正常,但对于  超过30张图像崩溃。

2 个答案:

答案 0 :(得分:0)

如果你看这里

  

http://developer.android.com/reference/android/graphics/BitmapFactory.html#decodeFile%28java.lang.String%29

您会发现decodeFile可以返回null。 因此,您应该在使用之前检查myBitmap

你也会发现this很有趣:

答案 1 :(得分:0)

For lesser number of image it works fine, but for more then 30 images it crashing

听起来你已经失去记忆..不像他在回答中提到的NPE那样。使用较小的图像并在需要时进行缩放。如果没有看到logcat,这将是我的第一个建议。希望它有所帮助。