MediaStore外部内容URI从SDCARD获取数据

时间:2018-04-17 07:13:55

标签: java android android-mediaplayer mediastore

我正在使用MediaStore和Cursor从指定的文件夹中获取音频文件。我想要的是,它应该从手机的内部存储器中获取歌曲列表,但它是从SDCARD获取的。虽然我尝试过使用INTERNAL_CONTENT_URI,但它根本没有得到任何东西。请看看,它有什么不对。任何帮助将不胜感激。

    String AudioFilePath = "%" +"/nexcox/voicerecorder/audio/" +"%";

    Uri uri= MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    String selection= MediaStore.Audio.Media.DATA +" LIKE ? ";
    Cursor cursor=context.getContentResolver().query(uri,null,selection,
            new String[]{AudioFilePath},
            null);


    if (cursor!=null){
        if (cursor.moveToFirst()){
            do {
                String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                String duration = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
                String sourceLocation = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));

                AudioInfo audioInfo=new AudioInfo(name,duration,sourceLocation);
                audioInfos.add(audioInfo);
            }while (cursor.moveToNext());
        }

        cursor.close();
        audioAdapter=new AudioAdapter(context,audioInfos);

        recyclerView.setAdapter(audioAdapter);
}

2 个答案:

答案 0 :(得分:1)

例如

     String fullpath = trackcursor.getString(trackcursor.getColumnIndex(MediaStore.Audio.Media.DATA))

if(fullpath.contains(Environment.getExternalStorageDirectory(){
// you have the internal sdcard
} 

在您的代码中:

        if (cursor!=null){
        if (cursor.moveToFirst()){
            do {
                String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                String duration = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
                String sourceLocation = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));

                    if(sourceLocation.contains(Environment.getExternalStorageDirectory(){
    // you have the internal sdcard
     AudioInfo audioInfo=new AudioInfo(name,duration,sourceLocation);
                audioInfos.add(audioInfo);
    } 

            }while (cursor.moveToNext());
        }

        cursor.close();
        audioAdapter=new AudioAdapter(context,audioInfos);
        recyclerView.setAdapter(audioAdapter);
}

答案 1 :(得分:0)

我使用与您的示例相同的方法。工作正常。

     public Cursor getMediastoreTrackDetails(Context context, String trackName) {
    if(context!=null) {
        ContentResolver cr = context.getContentResolver();
        final String _id = MediaStore.Audio.Media._ID;
        final String path = MediaStore.Audio.Media.DATA;
        final String album = MediaStore.Audio.Media.ALBUM;
        final String album_id = MediaStore.Audio.Media.ALBUM_ID;
        final String[] columns = {_id, path,album, album_id};
        final String[] trackname = {"%" + trackName + "%"};
        String where = path + " LIKE ?";
        return  cr.query(uri, columns, where, trackname, null);
    }else{
        return null;
    }
}