Gallery 应用程序如何如此快速地加载图像和视频?

How do Gallery app load Images and Videos so fast?

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

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

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);

            }
        }
    }

任何有关如何提高加载速度的建议或任何有关如何使此代码更高效的建议都将不胜感激

考虑 运行 您使用 CursorLoader to prevent blocking UI thread and obtaining live updates. I think answer to this question 的查询可能对您有用。

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

https://github.com/andriyadi/AndroidMediaChooser