如何立即获取文件资源?

时间:2016-04-08 18:15:35

标签: android performance

当我看到使用视频,照片,歌曲等文件的Android应用时,这些内容会在打开时显示出来。我试图在我的应用程序中查询所有这些,即使我只有100首歌曲,我也要等几百毫秒。当我想要获取图像或视频时,它需要大约相同的时间。 Google Play上看到的应用如何实现此加载速度?在加载应用程序和查找文件之前,他们还能够(例如)使用此信息(音乐播放器小部件)创建小部件。

我如何加载歌曲的示例:

final String[] mediaProjection = {
            MediaStore.Audio.Media._ID,
            MediaStore.Audio.Media.ALBUM_ID,
            MediaStore.Audio.Media.ARTIST_ID,
            MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Media.ALBUM,
            MediaStore.Audio.Media.ARTIST,
            MediaStore.Audio.Media.DURATION,
            MediaStore.Audio.Media.YEAR,
            MediaStore.Audio.Media.DATE_ADDED,
            MediaStore.Audio.Media.DATE_MODIFIED,
            MediaStore.Audio.Media.DATA,};

    ContentResolver songResolver = getContentResolver();
    Uri musicUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Cursor musicCursor = songResolver.query(musicUri, mediaProjection, null, null, null);

    if (musicCursor != null && musicCursor.moveToFirst()) {
        int idColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media._ID);
        int albumIdColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID);
        int artistIdColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID);
        int titleColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
        int albumColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM);
        int artistColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
        int durationColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.DURATION);
        int yearColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.YEAR);
        int dateAddedColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.DATE_ADDED);
        int dateModifiedColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.DATE_MODIFIED);
        int pathColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.DATA);

        do {
            long songId = musicCursor.getLong(idColumn);
            long songAlbumId = musicCursor.getLong(albumIdColumn);
            long songArtistId = musicCursor.getLong(artistIdColumn);
            String songTitle = musicCursor.getString(titleColumn);
            String songAlbum = musicCursor.getString(albumColumn);
            String songArtist = musicCursor.getString(artistColumn);
            long songDuration = musicCursor.getLong(durationColumn);
            int songYear = musicCursor.getInt(yearColumn);
            int songDateAdded = musicCursor.getInt(dateAddedColumn);
            int songDateModified = musicCursor.getInt(dateModifiedColumn);
            String songPath = musicCursor.getString(pathColumn);


                // Add these things into a list
                // ...
            }
        } while (musicCursor.moveToNext());
    }
    if (musicCursor != null) {
        musicCursor.close();
    }

    return true;

1 个答案:

答案 0 :(得分:0)

使用androids内容提供程序类快速将媒体文件加载到您的应用程序。

Android内容提供商充当应用程序和存储之间的中间层,易于使用。

因此,假设您要加载所有图像,然后只查询内容提供商媒体/外部/图像数据库,您就可以快速加载数据。音乐库的方式相同。