图像切换器的内存问题
我正在尝试从 SDCard 中的文件夹将一些图像加载到我的 ImageSwitcher 上。我正在创建一个可运行程序并在循环中执行,以便图像一个接一个地显示。
final ImageSwitcher imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher1);
imageSwitcher.postDelayed(
new Runnable() {
@Override
public void run() {
i++;
File dir = new File("/sdcard/Wallpapers/My Wallpapers");
final File file[]=dir.listFiles();
imageSwitcher.setImageURI(Uri.fromFile(file[i]));
imageSwitcher.postDelayed(this, 2000);
}
},
2000);
有些图像工作正常,一段时间后应用程序因“内存不足”异常而崩溃。
我尝试通过将此代码放在下面来解决内存问题:
“imageSwitcher.postDelayed(本,2000);”
这是我放在那里的代码:
ImageView v = (ImageView)imageSwitcher.getNextView();
BitmapDrawable bd = (BitmapDrawable) v.getDrawable();
Bitmap b = bd.getBitmap();
b.recycle();
此强制关闭应用程序并出现空指针异常。
我想知道如何处理这个内存问题......或者我哪里出了问题?
I'm trying to load some images onto my ImageSwitcher from a folder in SDCard. I'm creating a runnable and execute in a loop so that the images are displayed one after the other.
final ImageSwitcher imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher1);
imageSwitcher.postDelayed(
new Runnable() {
@Override
public void run() {
i++;
File dir = new File("/sdcard/Wallpapers/My Wallpapers");
final File file[]=dir.listFiles();
imageSwitcher.setImageURI(Uri.fromFile(file[i]));
imageSwitcher.postDelayed(this, 2000);
}
},
2000);
Some images work fine, and after a while the app crashes due to "outofmemory" exception.
I tried solving the memory issue by putting this code just below the:
"imageSwitcher.postDelayed(this, 2000);"
This was the code i put there:
ImageView v = (ImageView)imageSwitcher.getNextView();
BitmapDrawable bd = (BitmapDrawable) v.getDrawable();
Bitmap b = bd.getBitmap();
b.recycle();
This force closes the app with a Nullpointer exception.
I want to know how to handle this memory issue..or where am I going wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论