从Android的SD卡获取歌曲的路径(Get path of song from SD card

2019-07-31 07:32发布

在我的Android应用程序,我想获取从SD卡中的歌曲,但我没能得到那个特定file.I的路径正在使用Android的API级别7不支持下面的方法。

Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_MUSIC);

我也曾尝试下面的代码:

path = Environment.getExternalStorageDirectory();

但我不知道如何指定音乐file.Please的路径提出了一些solution.Thanx。

Answer 1:

获得从SD卡路径和歌曲名称。 你可以找到从MediaStore歌曲的路径。

媒体提供包含在内部和外部存储设备的所有可用的媒体元数据。

private String[] STAR = { "*" };

public void ListAllSongs() 
    { 
        Cursor cursor;
        Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";

        if (isSdPresent()) {
            cursor = getContentResolver().query(allsongsuri, STAR, selection, null, null);

            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    do {
                        String songname = 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));

                        String albumname = cursor.getString(cursor
                                .getColumnIndex(MediaStore.Audio.Media.ALBUM));

                    } while (cursor.moveToNext());
                }
                cursor.close();
            }
        }
    }


public static boolean isSdPresent() 
{
    return android.os.Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED);
}


Answer 2:

你可以扫描整个SD卡的任何文件格式,在这里我用MP3和MP4。
你可以用这个,ü需要的任何格式。

    /** To store the available media files */
    private List<String> mediaList = new ArrayList<String>();

    externalStoragePath = Environment.getExternalStorageDirectory()
                .getAbsolutePath();

        targetDir = new File(externalStoragePath);

        Log.d(" externalStoragePath ::: ", targetDir.getAbsolutePath());
public File[] mediaFiles = targetDir.listFiles();

/**
     * scanFiles
     * 
     * @param scanFiles
     */
    public void scanFiles(File[] scanFiles) {

        if (scanFiles != null) {
            for (File file : scanFiles) {

                if(mediaList.size() > 4){
                    return;
                }

                if (file.isDirectory()) {
                    // Log.d(" scaned File ::isDirectory: ",
                    // file.getAbsolutePath());
                    scanFiles(file.listFiles());

                } else {

                    addToMediaList(file);

                }

            }
        } else {

            Log.d(SCANNER,
                    " *************** No file  is available ***************");

        }
    }



/**
     * 
     * @param file
     */

    private void addToMediaList(File file) {

        if (file != null) {

            String path = file.getAbsolutePath();

            int index = path.lastIndexOf(".");

            String extn = path.substring(index + 1, path.length());

            if (extn.equalsIgnoreCase("mp4") || extn.equalsIgnoreCase("mp3")) {// ||

                Log.d(" scanned File ::: ", file.getAbsolutePath()
                        + "  file.getPath( )  " + file.getPath());// extn.equalsIgnoreCase("mp3"))
                                                                    // {
                Log.d(SCANNER, " ***** above file is added to list ");
                mediaList.add(path);


            }
        }

    }


文章来源: Get path of song from SD card in android