【问题标题】:How do Gallery app load Images and Videos so fast?图库应用程序如何快速加载图像和视频?
【发布时间】:2018-01-21 13:08:46
【问题描述】:

我正在尝试加载用户设备上存在的所有图像和视频,但问题是在某些设备上加载速度非常慢并且需要一些时间。如果手机同时运行多个应用程序,加载速度也会进一步降低。而预装的图库应用程序(始终)以更快的速度加载图像和视频。

这就是我加载图像和视频的方式-

public void getImages(Context context) {

        File Image_File;
        final String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID};

        String orderBy = MediaStore.Images.Media.DATE_MODIFIED ;

        Cursor cursor = context.getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
                null, orderBy);

        if (cursor != null)
        {
            cursor.moveToFirst();
            final int dataColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);

            while (cursor.moveToNext()) 
             {
                Image_File = new File(cursor.getString(dataColumnIndex));
                            images.add(0,Image_File);

              }
         }
}

    public void getVideos(Context context) {

        File Video_File;

        final String[] columns = { MediaStore.Video.Media.DATA, MediaStore.Video.Media._ID };
        String orderBy =  MediaStore.Video.Media.DATE_MODIFIED;

        Cursor cursor = context.getContentResolver().query(
                MediaStore.Video.Media.EXTERNAL_CONTENT_URI, columns, null,
                null, orderBy);

        if (cursor!=null)
        {
            cursor.moveToFirst();

            final int dataColumnIndex = cursor.getColumnIndex(MediaStore.Video.Media.DATA);

            while (cursor.moveToNext()) {
                Video_File = new File(cursor.getString(dataColumnIndex));

                            videos.add(0,Video_File);

            }
        }
    }

任何关于如何提高加载速度的建议或任何关于如何使这段代码更高效的建议将不胜感激和感激

【问题讨论】:

  • “在某些设备上加载非常缓慢并且需要一些时间”——使用方法跟踪来确定您将时间花在哪里。
  • @CommonsWare 感谢您的回答。您能否提供一个如何执行方法跟踪的示例。
  • @CommonsWare 感谢您的链接。会调查并更新。
  • @propoLis 仍在处理这个问题。

标签: android image performance video loading


【解决方案1】:

考虑使用 CursorLoader 运行查询,以防止阻塞 UI 线程并获取实时更新。我认为回复this question 可能对您有用。

【讨论】:

  • 感谢您的回答。我正在使用 AsyncTask 加载图像和视频。使用这种方法会减少加载时间吗?
【解决方案2】:

我做过这样的项目。检查链接将使您受益匪浅:

https://github.com/andriyadi/AndroidMediaChooser

【讨论】:

  • 非常感谢。将调查并更新您。
  • 浏览了这个库,发现它非常有用。感谢您制作图书馆。也会用它来学习其他东西。如果我遇到任何问题,会与您联系。只是想确认由于该库是开源的,我可以在我的应用程序中使用该库中的代码吗?
  • 是的,这个库是开源的,您可以按照自己的意愿实现您的项目
猜你喜欢
  • 2017-12-04
  • 1970-01-01
  • 2018-08-16
  • 1970-01-01
  • 1970-01-01
  • 2015-04-22
  • 2017-10-20
  • 2013-04-03
  • 2018-12-29
相关资源
最近更新 更多