Android & 读取MP3文件的图片

MainActivity.java
package com.example.zachary01;

import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.ContentUris;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.Window;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity {
    ListView musiclv;
    public static final ArrayList<MusicInfo> musicList= new ArrayList<MusicInfo>();
    int musiclen = 0;
    public boolean isPlaying = false;
    private Button netButton = null;
    public static Resources resources;
    static int musicID = 1;
    private static final Uri sArtworkUri = Uri
            .parse("content://media/external/audio/media/" + musicID
                    + "/albumart");
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        Init();
    }

    public void Init(){
        musiclv = (ListView)findViewById(R.id.musiclist);
        resources = this.getResources(); // 获取资源
        netButton = (Button)findViewById(R.id.net_button);
        getMusic();
        listViewInit();
    }
    
    private void getMusic(){
        musicList.clear();// 存入前先清空列表(处理返回桌面再进Acitvity 再次读取列表*2的问题)
        // 取得数据库对象
        // 查询所有音乐信息
        Cursor cursor = this.getContentResolver().query(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[] {
                        // 歌曲名0
                        MediaStore.Audio.Media.TITLE,
                        // 歌手名1
                        MediaStore.Audio.Media.ARTIST,
                        // 专辑名2
                        MediaStore.Audio.Media.ALBUM,
                        // 歌曲路径3
                        MediaStore.Audio.Media.DATA,
                        // 歌曲时间4
                        MediaStore.Audio.Media.DURATION,
                        // 歌曲ID5
                        MediaStore.Audio.Media._ID,
                        // 歌曲大小6
                        MediaStore.Audio.Media.SIZE,
                        
                        // 显示全部名字 可以区分什么格式比如.mp3 .wma
                        MediaStore.Audio.Media.DISPLAY_NAME, },
                // 将较小音频文件过滤掉
                MediaStore.Audio.Media.SIZE + ">='" + 819200 + "'", null, null);
        
        if(cursor!=null && cursor.moveToFirst()){
            do{
                MusicInfo mi = new MusicInfo(cursor.getString(0), 
                        cursor.getString(1),
                        cursor.getString(2),
                        cursor.getString(3),
                        cursor.getLong(4),
                        (int)cursor.getLong(5),
                        cursor.getString(6)
                        );
                Bitmap bm = getMusicBitmap(this, mi.getId(), -1);
                if(bm == null)
                    bm = BitmapFactory.decodeResource(resources, R.drawable.music);
                mi.setBm(bm);
                musicList.add(mi);
            }while(cursor.moveToNext());
        }
        musiclen = musicList.size();
    }
    
    /**
     * 将MP3里图片读取出来
     * 
     * @param context
     * @param songid
     * @param albumid
     * @return
     */
    private static Bitmap getMusicBitmap(Context context, long songid,
            long albumid) {
        Bitmap bm = BitmapFactory.decodeResource(resources, R.drawable.music);
        // 判断相关数据
        if (albumid < 0 && songid < 0) {
            throw new IllegalArgumentException(
                    "Must specify an album or a song id");
        }
        try {
            if (albumid < 0) {
                Uri uri = Uri.parse("content://media/external/audio/media/"
                        + songid + "/albumart");
                ParcelFileDescriptor pfd = context.getContentResolver()
                        .openFileDescriptor(uri, "r");
                if (pfd != null) {
                    FileDescriptor fd = pfd.getFileDescriptor();
                    bm = BitmapFactory.decodeFileDescriptor(fd);
                }
                else{
                    bm = BitmapFactory.decodeResource(resources, R.drawable.music);
                }
            } else {
                Uri uri = ContentUris.withAppendedId(sArtworkUri, albumid);
                ParcelFileDescriptor pfd = context.getContentResolver()
                        .openFileDescriptor(uri, "r");
                if (pfd != null) {
                    FileDescriptor fd = pfd.getFileDescriptor();
                    bm = BitmapFactory.decodeFileDescriptor(fd);
                }
            }
        } catch (FileNotFoundException ex) {
        }
        return bm;
    }
    public void listViewInit(){
        ListAdapter listAdapter = new ListAdapter(this, musicList);
        musiclv.setAdapter(listAdapter);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
ListAdapter.java
package com.example.zachary01;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListAdapter extends BaseAdapter {

    Context con;
    ArrayList<MusicInfo> list;

    public ListAdapter(Context con, ArrayList<MusicInfo> list) {
        this.con = con;
        this.list = list;
    }

    public int getCount() {

        return this.list.size();
    }

    public Object getItem(int arg0) {
        return this.list.get(arg0);
    }

    public long getItemId(int arg0) {

        return arg0;
    }

    public View getView(int arg0, View arg1, ViewGroup arg2) {
        View view = arg1;
        if (view == null) {
            view = LayoutInflater.from(con).inflate(R.layout.list, null);
        }
        ImageView img = (ImageView)view.findViewById(R.id.img);
        TextView title = (TextView)view.findViewById(R.id.title);
        TextView artist = (TextView)view.findViewById(R.id.artist);
        
        // -------------------------------------
        img.setImageBitmap(this.list.get(arg0).getBm());
        title.setText(this.list.get(arg0).getTitle());
        artist.setText(this.list.get(arg0).getArtist());
        return view;
    }

}
MusicInfo.java
package com.example.zachary01;

import android.graphics.Bitmap;

public class MusicInfo {
        private Bitmap bm;//专辑照片
        private String title;
        private String artist;
        private String album;
        private String url;
        private long time;
        private int id;
        private String size;
        public MusicInfo(String title,  String artist, String album, String url, long time, int id, String size){
            this.title = title;
            this.artist =artist;
            this.album = album;
            this.url = url;
            this.time = time;
            this.id = id;
            this.size = size;
        }
     
        public String getTitle() {
            return title;
        }
        public void setTitle(String title) {
            this.title = title;
        }
     
        public String getArtist() {
            return artist;
        }
        public void setArtist(String artist) {
            this.artist = artist;
        }
        public String getAlbum() {
            return album;
        }
        public void setAlbum(String album) {
            this.album = album;
        }
        public String getUrl() {
            return url;
        }
        public void setUrl(String url) {
            this.url = url;
        }
        public Bitmap getBm() {
            return bm;
        }
        public void setBm(Bitmap bm) {
            this.bm = bm;
        }
        public String getSize() {
            return size;
        }
        public void setSize(String size) {
            this.size = size;
        }
        public long getTime() {
            return time;
        }
        public void setTime(long time) {
            this.time = time;
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        @Override
        public String toString() {
            return "MusicInfo [title=" + title  + ", artist="
                 + artist + ", album=" + album +", url=" + url +", getClass()=" + getClass()
                 + "]";
        }
     
 }
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/background"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <LinearLayout    android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/word"
        >
        <TextView    android:layout_width="180sp"
            android:layout_height="40sp"
            android:gravity="center_vertical"
            android:textSize="25sp"
            android:text="@string/app_name"
            android:textColor="@color/word"
            android:background="@color/grey"
            android:layout_marginTop="0dp"
            />

        <Button
            android:id="@+id/local_button"
            android:layout_width="70sp"
            android:layout_height="40dp"
            android:layout_marginBottom="0dp"
            android:layout_marginLeft="1dp"
            android:layout_marginRight="1dp"
            android:layout_marginTop="-4dp"
            android:background="@color/grey"
            android:gravity="center"
            android:text="@string/local"
            android:textColor="@color/word"
            android:textSize="18sp" />

        <Button
            android:id="@+id/net_button"
            android:layout_width="70sp"
            android:layout_height="40dp"
            android:layout_marginBottom="0dp"
            android:layout_marginLeft="0dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="-4dp"
            android:background="@color/grey"
            android:gravity="center"
            android:text="@string/net"
            android:textColor="@color/word"
            android:textSize="18sp" />

    </LinearLayout>
   <ListView    android:id="@+id/musiclist"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>
</LinearLayout>
list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_vertical"
    android:orientation="horizontal" >
    
    <ImageView
        android:id="@+id/img"
        android:layout_width="60sp"
        android:layout_height="60sp"
        />"

    <LinearLayout    
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        
        <TextView    android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="43sp"
            android:textSize="30sp"
            />
        
        <TextView    android:id="@+id/artist"
            android:layout_width="wrap_content"
            android:layout_height="17sp"
            android:textSize="13sp"
            />"
    </LinearLayout>

</LinearLayout>

 

posted on 2013-05-07 12:45  Zachary_wiz  阅读(744)  评论(0编辑  收藏  举报

导航