获得Android设备的mp3格式的文件 [英] Get .mp3 format files from android device
问题描述
我正在开发一个音乐播放器。我想从Android设备上获取所有的mp3格式的文件。
但是,这code未得到任何文件。我的.mp3文件是在SD卡/音乐文件夹中。如果我更改 MEDIA_PATH =新的String(SD卡/音乐);
这样它只能从音乐文件夹获取文件。但我需要让我的外部/内部MEMORYCARD每个所有.mp3文件和每个文件夹。请帮帮我。
这是我的code。
最后弦乐MEDIA_PATH =新的String(Environment.getExternalStorageDirectory());公共无效Generate_Database(){
文件首页=新的文件(MEDIA_PATH); 如果(home.listFiles(新FileExtensionFilter())长度方式> 0){
对于(文件文件:home.listFiles(新FileExtensionFilter())){
。字符串title = file.getName()子(0,(file.getName()长() - 4));
字符串路径= file.getPath();
mediaInfo.setDataSource(路径); 字符串ALBUMNAME =未知,艺术家=未知,genere =未知,持续时间=未知,作曲家=未知; 如果(mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM)== NULL)
ALBUMNAME =未知;
其他{
ALBUMNAME = mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM);
}
如果(mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST)== NULL)
艺术家=未知;
其他{
艺术家= mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
}
如果(mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE)== NULL)
genere =未知;
其他{
genere = mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE);
}
如果(mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)== NULL)
持续时间=未知;
其他{
持续时间= mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
}
如果(mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_COMPOSER)== NULL)
作曲家=未知;
其他{
作曲家= mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_COMPOSER);
} //Toast.makeText(getApplicationContext(),标题+路径+ ALBUMNAME +艺术家+ genere +时间+作曲,Toast.LENGTH_LONG).show(); ds.createEntry2(标题,路径,ALBUMNAME,艺术家,genere,持续时间,作曲家);
}
}
这是用来提取.mp3文件
类FileExtensionFilter实现了FilenameFilter {
公共布尔接受(文件目录,字符串名称){
回报(name.endsWith(MP3)|| name.endsWith()MP3);
}}
下面将帮助你在所有设备中的音频文件:
私人无效scanSdcard(){
!= 0字符串选择= MediaStore.Audio.Media.IS_MUSIC +;
的String [] =投影{
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DURATION
};
最后弦乐中将sortOrder = MediaStore.Audio.AudioColumns.TITLE +分页中局部ASC; 光标光标= NULL;
尝试{
URI URI = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
光标= getActivity()getContentResolver()查询(URI,投影,选择,空,中将sortOrder)。
如果(指针!= NULL){
cursor.moveToFirst();
而(!cursor.isAfterLast()){
MediaData媒体=新MediaData();
字符串title = cursor.getString(0);
串艺术家= cursor.getString(1);
字符串路径= cursor.getString(2);
串的displayName = cursor.getString(3);
串songDuration = cursor.getString(4);
cursor.moveToNext();
} } }赶上(例外五){
log.e(TAG,e.toString(),E);
}最后{
如果(指针!= NULL){
cursor.close();
}
}
}
I'm developing a music player. I want to get all .mp3 format files from android device.
But this code is not getting any files. My .mp3 files are in 'sdcard/music' folder. If i change the MEDIA_PATH = new String("sdcard/music");
like this it's getting files only from that music folder. But i need to get all .mp3 files from each and every folder in my external/internal memorycard. Please help me.
this is my code.
final String MEDIA_PATH = new String(Environment.getExternalStorageDirectory());
public void Generate_Database(){
File home = new File(MEDIA_PATH);
if (home.listFiles(new FileExtensionFilter()).length > 0) {
for (File file : home.listFiles(new FileExtensionFilter())) {
String title = file.getName().substring(0, (file.getName().length() - 4));
String path = file.getPath();
mediaInfo.setDataSource(path);
String albumName = "unknown",artist = "unknown",genere = "unknown",duration = "unknown",composer = "unknown";
if(mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM) == null)
albumName = "unknown";
else{
albumName = mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM);
}
if(mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST) == null)
artist = "unknown";
else{
artist = mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
}
if(mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE) == null)
genere = "unknown";
else{
genere = mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE);
}
if(mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION) == null)
duration = "unknown";
else{
duration = mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
}
if(mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_COMPOSER) == null)
composer = "unknown";
else{
composer = mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_COMPOSER);
}
//Toast.makeText(getApplicationContext(), title+path+ albumName+artist+ genere+ duration+ composer, Toast.LENGTH_LONG).show();
ds.createEntry2(title, path, albumName, artist, genere, duration, composer);
}
}
this is used to extract the .mp3 files
class FileExtensionFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.endsWith(".mp3") || name.endsWith(".MP3"));
}
}
The following will get all the audio files in your device:
private void scanSdcard(){
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
String[] projection = {
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DURATION
};
final String sortOrder = MediaStore.Audio.AudioColumns.TITLE + " COLLATE LOCALIZED ASC";
Cursor cursor = null;
try {
Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
cursor = getActivity().getContentResolver().query(uri, projection, selection, null, sortOrder);
if( cursor != null){
cursor.moveToFirst();
while( !cursor.isAfterLast() ){
MediaData media = new MediaData();
String title = cursor.getString(0);
String artist = cursor.getString(1);
String path = cursor.getString(2);
String displayName = cursor.getString(3);
String songDuration = cursor.getString(4);
cursor.moveToNext();
}
}
} catch (Exception e) {
log.e(TAG, e.toString(), "e");
}finally{
if( cursor != null){
cursor.close();
}
}
}
这篇关于获得Android设备的mp3格式的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!