我需要在listview中从sdcard获取音乐专辑的元数据

时间:2015-08-28 07:22:27

标签: android metadata

import android.app.Activity;  
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color; 
import android.media.MediaMetadataRetriever; 
import android.os.Bundle;
import android.widget.ImageView; 
import android.widget.TextView; 

public class MetaExtractActivity extends Activity { ImageView album_art; 

TextView album, artist, genre; MediaMetadataRetriever metaRetriever; 
byte[] art;

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
getInit(); 

// Ablum_art retrieval code 
// metaRetriever = new MediaMetadataRetriever(); 

metaRetriever.setDataSource("/sdcard/audio.mp3"); 

try 
{
    art = metaRetriever.getEmbeddedPicture();
    Bitmap songImage = BitmapFactory .decodeByteArray(art, 0, art.length); album_art.setImageBitmap(songImage); 
    album.setText(metaRetriever .extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM)); artist.setText(metaRetriever .extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST)); genre.setText(metaRetriever .extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE));
 } 
 catch (Exception e) { album_art.setBackgroundColor(Color.GRAY); 

 album.setText("Unknown Album"); 

 artist.setText("Unknown Artist");
 genre.setText("Unknown Genre"); 
} } 

// Fetch Id' s form xml public void getInit(){album_art =(ImageView)findViewById(R.id.album_art);

album = (TextView) findViewById(R.id.Album); 

artist = (TextView) findViewById(R.id.artist_name); 
genre = (TextView) findViewById(R.id.genre);
 } }

1 个答案:

答案 0 :(得分:0)

您可以使用资源库Apache Tika,这将帮助您获取文件的可用信息,只要它可用。

  

注意:无法保证您的文件包含您需要的元数据。

使用Java的相关代码段:

    public static void main(String[] args) {
            String fileLocation = "C:/desktopname/songs/song.mp3";

            try {

            InputStream input = new FileInputStream(new File(fileLocation));
            ContentHandler handler = new DefaultHandler();
            Metadata metadata = new Metadata();
            Parser parser = new Mp3Parser();
            ParseContext parseCtx = new ParseContext();
            parser.parse(input, handler, metadata, parseCtx);
            input.close();

            // List all metadata
            String[] metadataNames = metadata.names();

            for(String name : metadataNames){
            System.out.println(name + ": " + metadata.get(name));
            }

            // Retrieve the necessary info from metadata
            // Names - title, xmpDM:artist etc. - mentioned below may differ based
            System.out.println("----------------------------------------------");
            System.out.println("Title: " + metadata.get("title"));
            System.out.println("Artists: " + metadata.get("xmpDM:artist"));
            System.out.println("Composer : "+metadata.get("xmpDM:composer"));
            System.out.println("Genre : "+metadata.get("xmpDM:genre"));
            System.out.println("Album : "+metadata.get("xmpDM:album"));

            } catch (Exception e) { } 

    }

要在Android中使用此功能,您只需要获取文件的正确URI,然后使用上面的代码。