我有了这个小功能,可让我从path
名为加载图像name
。它奏效了,问题是我不得不多次打电话给我,而不是密不可分。比方说,在特定活动的负载下进行了十二次。加载它们都需要几秒钟。
最佳吗?有没有更简单的方法来达到相同的结果?
public static Bitmap loadImageFrom(File path, String name)
{
try {
File f = new File(path, name);
return BitmapFactory.decodeStream(new FileInputStream(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}
我试过这样添加if(!f.exists()) return null;
:
public static Bitmap loadImageFrom(File path, String name)
{
try {
File f = new File(path, name);
if(!f.exists()) return null;
return BitmapFactory.decodeStream(new FileInputStream(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}
我知道这是多余的,但是我无法删除Try Catch子句,如果这样做,它会给我一个错误。但是,没有注意到加速。
有什么建议吗?
据我所知,并不是代码效率低下,在主线程上手动加载图片会损害应用程序的性能。尤其是当您有大量图片和/或使用它们填充您的ListView
或时GridView
。
看看Picasso或Universal Image Loader,这些库应有助于有效地加载图片,从而加快您的应用程序的速度。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句