Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用光标将封面艺术嵌入到音频文件中?_Android_Android Contentprovider_Android Cursor - Fatal编程技术网

Android 如何使用光标将封面艺术嵌入到音频文件中?

Android 如何使用光标将封面艺术嵌入到音频文件中?,android,android-contentprovider,android-cursor,Android,Android Contentprovider,Android Cursor,我正在开发一个非常小的音频播放器。问题是:在许多问题之后,我设法用从外部存储中的任何文件夹检索的歌曲构建了一个列表视图,并将它们与标题、艺术家和专辑名一起列出。现在我想在ListView中添加封面相册。封面必须取自歌曲音频文件中嵌入的图像 我尝试使用MediaMetadataRetriever,但无法获取每个文件的完整Uri,因此无法为其设置数据源。我怎样才能拿到封面?如果我有字节数组,我会使用位图工厂。。。但我没有 顺便说一句,这是我的代码。。。 在“活动”中,这是在外部存储器中搜索音频文件并

我正在开发一个非常小的音频播放器。问题是:在许多问题之后,我设法用从外部存储中的任何文件夹检索的歌曲构建了一个列表视图,并将它们与标题、艺术家和专辑名一起列出。现在我想在ListView中添加封面相册。封面必须取自歌曲音频文件中嵌入的图像

我尝试使用MediaMetadataRetriever,但无法获取每个文件的完整Uri,因此无法为其设置数据源。我怎样才能拿到封面?如果我有字节数组,我会使用位图工厂。。。但我没有

顺便说一句,这是我的代码。。。 在“活动”中,这是在外部存储器中搜索音频文件并将其放入列表中的空白:

public void retrieveAudioFiles(){
        songsList = new SongsList();

        Uri sd = Audio.Media.EXTERNAL_CONTENT_URI;
        String[] cols = {Audio.Media.TITLE,Audio.Media.ARTIST,Audio.Media.ALBUM};
        String where = Audio.Media.IS_MUSIC;
        Cursor audioCursor = getContentResolver().query(sd,cols,where,null,null);

        while (audioCursor.moveToNext()){
            int posColTitle = audioCursor.getColumnIndex(Audio.Media.TITLE);
            int posColArtist = audioCursor.getColumnIndex(Audio.Media.ARTIST);
            int posColAlbum = audioCursor.getColumnIndex(Audio.Media.ALBUM);

            String songTitle = audioCursor.getString(posColTitle);
            String songArtist = audioCursor.getString(posColArtist);
            String songAlbum = audioCursor.getString(posColAlbum);

            songsList.add(new Song(songTitle,songArtist,songAlbum));
            }
        audioCursor.close();
    }
这是适配器:

public class SongsAdapter extends BaseAdapter {

    private SongsList songsList;
    private LayoutInflater songInf;

    public SongsAdapter(Context c, SongsList theSongs){
        super();  
        songsList=theSongs;
        songInf=LayoutInflater.from(c);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return songsList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return songsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        RowWrapper wrapper;
        if (convertView == null)
        {
            convertView = songInf.inflate(
                R.layout.song_row, null);
            wrapper = new RowWrapper(convertView);
            convertView.setTag(wrapper);
        }
        else
        {
            wrapper = (RowWrapper) convertView.getTag();
        }
        Song song = (Song) getItem(position);
        wrapper.populate(song);

        return convertView;
    }

    private static class RowWrapper
    {
        private TextView titleTextView;
        private TextView artistTextView;
        private TextView albumTextView;
        //private ImageView coverImageView;

        public RowWrapper(View convertView)
        {
            titleTextView = (TextView) convertView.findViewById(R.id.textTitle);
            artistTextView = (TextView) convertView.findViewById(R.id.textArtist);
            albumTextView = (TextView) convertView.findViewById(R.id.textAlbum);
            //coverImageView = (ImageView) convertView.findViewById(R.id.smallCover);
        }

        public void populate(Song song)
        {
            titleTextView.setText(song.title);
            artistTextView.setText(song.artist);
            albumTextView.setText(song.album);
            //if (song.cover != null)
            //coverImageView.setImageBitmap(song.cover);
        }
    }

}
这是歌曲课:

public class Song {

    public String title="";
    public String artist="";
    public String album="";
    public Bitmap cover=null;

    public Song(String t, String ar, String al){
        title=t;
        artist=ar;
        album=al;
        //cover=c;
    }

    public Song(){

    }

}
这是SongsList类(一个简单的歌曲数组列表):

公共类歌曲列表扩展了ArrayList{
公共歌曲列表(){
超级();
}
}
这是舱单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.audioplayer"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.audioplayer.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

这是ListActivity单行的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical"
    android:onClick="songPicked" >

    <ImageView
        android:id="@+id/smallCover"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:contentDescription="@string/coverDescription"
        android:src="@drawable/no_cover" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/labelTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/labelTitle" />

        <TextView
            android:id="@+id/textTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:text="@string/textTitle" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/labelArtist"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dp"
            android:text="@string/labelArtist" />

        <TextView
            android:id="@+id/textArtist"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:text="@string/textArtist" />

        <TextView
            android:id="@+id/labelAlbum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="@string/labelAlbum" />

        <TextView
            android:id="@+id/textAlbum"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:text="@string/textAlbum" />
    </LinearLayout>

</LinearLayout>

希望你能帮我…

我自己做的

我在Song类中为id添加了一个长参数,为path添加了一个Uri参数

在活动中,就在songsList.add(新歌(……)(我在构造函数中添加了位图参数)之前,我添加了以下说明:

int posColId = audioCursor.getColumnIndex(Audio.Media._ID);
long songId = audioCursor.getLong(posColId);
Uri songUri = ContentUris.withAppendedId(Audio.Media.EXTERNAL_CONTENT_URI,songId);
String[] dataColumn = {Audio.Media.DATA};
Cursor coverCursor = getContentResolver().query(songUri, dataColumn, null, null, null);
coverCursor.moveToFirst();
int dataIndex = coverCursor.getColumnIndex(Audio.Media.DATA);
String filePath = coverCursor.getString(dataIndex);
coverCursor.close();
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(filePath);
byte[] coverBytes = retriever.getEmbeddedPicture();
Bitmap songCover;
if (coverBytes!=null) //se l'array di byte non è vuoto, crea una bitmap
    songCover = BitmapFactory.decodeByteArray(coverBytes, 0, coverBytes.length);
else
    songCover=null;
然后我取消了Song类和适配器中的指令注释

int posColId = audioCursor.getColumnIndex(Audio.Media._ID);
long songId = audioCursor.getLong(posColId);
Uri songUri = ContentUris.withAppendedId(Audio.Media.EXTERNAL_CONTENT_URI,songId);
String[] dataColumn = {Audio.Media.DATA};
Cursor coverCursor = getContentResolver().query(songUri, dataColumn, null, null, null);
coverCursor.moveToFirst();
int dataIndex = coverCursor.getColumnIndex(Audio.Media.DATA);
String filePath = coverCursor.getString(dataIndex);
coverCursor.close();
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(filePath);
byte[] coverBytes = retriever.getEmbeddedPicture();
Bitmap songCover;
if (coverBytes!=null) //se l'array di byte non è vuoto, crea una bitmap
    songCover = BitmapFactory.decodeByteArray(coverBytes, 0, coverBytes.length);
else
    songCover=null;