【问题标题】:List all of one file type on Android device?列出 Android 设备上的所有一种文件类型?
【发布时间】:2023-04-06 08:27:02
【问题描述】:

例如,我想检索内部和外部存储中所有 .mp3 文件的列表。我还想要每个 .mp3 文件的路径 (String) 以供参考(将它们存储在 List 中?)我该怎么做?这是一个相对昂贵的操作吗?如果是这样,是否应该将其放在线程上并在线程运行时向用户显示“正在加载”ProgressDialog

【问题讨论】:

    标签: java android multithreading file


    【解决方案1】:

    请在下面找到函数。它将获取存储在外部和内部存储器中的所有 mp3 文件。

    public void getAllSongs() {
    
        Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
    
        cursor = managedQuery(allsongsuri, STAR, selection, null, null);
    
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                do {
                    song_name = cursor
                            .getString(cursor
                                    .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                    int song_id = cursor.getInt(cursor
                            .getColumnIndex(MediaStore.Audio.Media._ID));
    
                    String fullpath = cursor.getString(cursor
                            .getColumnIndex(MediaStore.Audio.Media.DATA));
                    fullsongpath.add(fullpath);
    
                    album_name = cursor.getString(cursor
                            .getColumnIndex(MediaStore.Audio.Media.ALBUM));
                    int album_id = cursor.getInt(cursor
                            .getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
    
                    artist_name = cursor.getString(cursor
                            .getColumnIndex(MediaStore.Audio.Media.ARTIST));
                    int artist_id = cursor.getInt(cursor
                            .getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));
    
    
                } while (cursor.moveToNext());
    
            }
            cursor.close();
            db.closeDatabase();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-11-26
      • 2017-03-31
      • 2016-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多