简体   繁体   English

在android中获取SD卡的歌曲路径

[英]Get path of song from SD card in android

In my android application I want to fetch song from SD card but I am not able to get the path of that particular file.I am using android api level 7 which doesn't support following method. 在我的Android应用程序中,我想从SD卡获取歌曲,但我无法获得该特定文件的路径。我使用的是android api level 7,它不支持以下方法。

Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_MUSIC);

I have also tried following code : 我也试过以下代码:

path = Environment.getExternalStorageDirectory();

but I don't know how to specify path of music file.Please suggest some solution.Thanx. 但我不知道如何指定音乐文件的路径。请提出一些解决方案。谢谢。

Get path and song Name from SD Card. 从SD卡获取路径和歌曲名称。 You can find the path of the song from MediaStore. 您可以从MediaStore中找到该歌曲的路径。

The Media provider contains meta data for all available media on both internal and external storage devices. 媒体提供程序包含内部和外部存储设备上所有可用媒体的元数据。

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

you can scan the entire sd card for any file format,here i used for mp3 and mp4. 你可以扫描整个SD卡的任何文件格式,这里我用于MP3和MP4。
you can use this for any format that u required. 您可以将此用于您需要的任何格式。

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


            }
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM