问题
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.
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.
回答1:
Get path and song Name from SD Card. You can find the path of the song from 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);
}
回答2:
you can scan the entire sd card for any file format,here i used for mp3 and 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);
}
}
}
来源:https://stackoverflow.com/questions/12227023/get-path-of-song-from-sd-card-in-android