Android 中的快速缩略图检索

发布于 2024-12-21 09:19:08 字数 1339 浏览 0 评论 0原文

我创建了一个照片库,允许您选择多张照片,但我注意到用缩略图填充 GridView 可能会非常慢。我使用计时器:

timer = new Timer();
timer.schedule(new TimerTask() {        
    @Override
    public void run() {
        fetchNextThumb();
    }
}, 0, 25);

它在 GridView 中搜索没有缩略图的子项(我将其禁用),从 MediaStore 检索正确的缩略图(如果尚未缓存),并在 UI 线程上执行 setImageBitmap():

private void fetchNextThumb()
{
    for (int i = 0; i < gridView.getChildCount(); i++)
    {
        ImageSelectView isv = (ImageSelectView)gridView.getChildAt(i);
        if (isv == null)
            continue;
        if (isv.isEnabled())
            continue;
        activeThumb = thumbCache.get(isv.getId());
        if (activeThumb == null)
        {
            activeThumb = MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(), isv.getId(), MediaStore.Images.Thumbnails.MICRO_KIND, null);
            thumbCache.put(isv.getId(), activeThumb);
        }
        activeView = isv;
        runOnUiThread(setNextThumb);
        return;
    }
}
private Bitmap activeThumb;
private ImageSelectView activeView;
private Runnable setNextThumb = new Runnable() {
    public void run() {
        activeView.setImageBitmap(activeThumb);
        activeView.invalidate();
        activeView.setEnabled(true);
    }
};

一开始速度非常快,但开始抓取大约 50 个缩略图后速度会变慢。有人知道加快缩略图检索速度的技巧吗?

I've created a photo gallery that allows you to select multiple photos, but I've noticed that populating the GridView with thumbnails can be very slow. I use a timer:

timer = new Timer();
timer.schedule(new TimerTask() {        
    @Override
    public void run() {
        fetchNextThumb();
    }
}, 0, 25);

It searches the GridView for children without thumbnails (which I keep disabled), retrieves the proper thumb from MediaStore if it's not already cached, and performs setImageBitmap() on the UI thread:

private void fetchNextThumb()
{
    for (int i = 0; i < gridView.getChildCount(); i++)
    {
        ImageSelectView isv = (ImageSelectView)gridView.getChildAt(i);
        if (isv == null)
            continue;
        if (isv.isEnabled())
            continue;
        activeThumb = thumbCache.get(isv.getId());
        if (activeThumb == null)
        {
            activeThumb = MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(), isv.getId(), MediaStore.Images.Thumbnails.MICRO_KIND, null);
            thumbCache.put(isv.getId(), activeThumb);
        }
        activeView = isv;
        runOnUiThread(setNextThumb);
        return;
    }
}
private Bitmap activeThumb;
private ImageSelectView activeView;
private Runnable setNextThumb = new Runnable() {
    public void run() {
        activeView.setImageBitmap(activeThumb);
        activeView.invalidate();
        activeView.setEnabled(true);
    }
};

It's incredibly quick at first, but starts slowing down after about 50 or so thumbnails have been grabbed. Anyone know any tricks to speeding up thumbnail retrieval?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陪你搞怪i 2024-12-28 09:19:08

好吧,我明白为什么有些缩略图加载速度比其他缩略图快。我正在使用

managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{ MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID }, null, null, MediaStore.Images.Media._ID);

它显然可以从 SD 卡和设备中获取图像。毫不奇怪,SD 卡图像的拇指加载速度比设备上的图像慢得多。不过,内置图库似乎没有这个问题。我现在的解决方案是在加载一次 SD 卡图像后保存设备上的缩略图。

对于任何有兴趣实现自定义照片库的人来说,上面的代码应该可以很好地工作。有任何问题请随时给我留言。

Well, I figured out why some thumbnails are loading faster than others. I'm using

managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{ MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID }, null, null, MediaStore.Images.Media._ID);

which apparently grabs images from both the SD card and the device. Unsurprisingly, the thumbs for SD card images load way slower than those on the device. Still though, the built-in gallery doesn't seem to have this problem. My solution for now will be to save on-device thumbnails for SD card images after loading them once.

For anyone interested in implementing a custom photo gallery, the code above should work pretty well. Feel free to message me with any questions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文