4

我正在尝试创建一个仅提供 edittext 和 imagebutton 的应用程序。如果按钮被单击,则想法是将专辑添加到播放列表中,并在编辑文本框中命名。专辑应随机选择。不用说专辑曲目应该按正确的顺序排列。我可以稍后添加更多功能,例如。保存,覆盖,删除等。我有界面,但正在努力处理代码。我有点理解 ContentProviders 的概念。所以代码需要:

  • 访问播放列表,我相信这是通过使用 MediaStore.Audio.Playlists 实现的

  • 访问专辑,我相信这是通过使用 MediaStore.Audio.Albums 实现的

  • 添加到播放列表

我有以下代码(从该站点获得的大多数位。谢谢顺便说一句)来访问播放列表,但它因 Null Exception 错误而崩溃。

public void checkforplaylists()
{
    //Get a cursor over all playlists.
    final ContentResolver resolver= MediaProvider.mContentResolver;
    final Uri uri=MediaStore.Audio.Playlists.INTERNAL_CONTENT_URI;
    final String id=MediaStore.Audio.Playlists._ID;
    final String name=MediaStore.Audio.Playlists.NAME;
    final String[]columns={id,name};
    final Cursor playlists= resolver.query(uri, columns, null, null, null);

        if(playlists==null)
            {
                Log.e(TAG,"Found no playlists.");
                    return;
            }
    return;
}

有谁能帮忙吗?

4

3 回答 3

1

我认为您的意思是NullPointerException,这意味着您的一项分配为空,然后您尝试访问您想要的对象的成员。很可能是resolver,但要确保您需要报告的行号和/或使用调试器逐步完成。

于 2012-08-17T15:39:28.390 回答
1

This works. When using the ContentResolver, the Context (this) is required.

public void checkforplaylists(Context context)
{
    ContentResolver cr = context.getContentResolver();
    final Uri uri=MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
    final String id=MediaStore.Audio.Playlists._ID;
    final String name=MediaStore.Audio.Playlists.NAME;
    final String[]columns={id,name};
    final Cursor playlists= cr.query(uri, columns, null, null, null);
        if(playlists==null)
            {
                Log.e(TAG,"Found no playlists.");
                    return;
            }
        Log.e(TAG,"Found playlists.");
    return;
}
于 2012-08-23T18:56:52.397 回答
1

使用此代码,将工作

public boolean addPlaylist(String pname) {


    Uri playlists = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
    Cursor c = resolver.query(playlists, new String[] { "*" }, null, null,
            null);
    long playlistId = 0;
    c.moveToFirst();
    do {
        String plname = c.getString(c
                .getColumnIndex(MediaStore.Audio.Playlists.NAME));
        if (plname.equalsIgnoreCase(pname)) {
            playlistId = c.getLong(c
                    .getColumnIndex(MediaStore.Audio.Playlists._ID));
            break;
        }
    } while (c.moveToNext());
    c.close();

    if (playlistId != 0) {
        Uri deleteUri = ContentUris.withAppendedId(playlists, playlistId);
        Log.d(TAG, "REMOVING Existing Playlist: " + playlistId);

        // delete the playlist
        resolver.delete(deleteUri, null, null);
    }

    Log.d(TAG, "CREATING PLAYLIST: " + pname);
    ContentValues v1 = new ContentValues();
    v1.put(MediaStore.Audio.Playlists.NAME, pname);
    v1.put(MediaStore.Audio.Playlists.DATE_MODIFIED,
            System.currentTimeMillis());
    Uri newpl = resolver.insert(playlists, v1);
    Log.d(TAG, "Added PlayLIst: " + newpl);

    flag=true;
    return flag;
}
于 2015-06-12T05:11:35.657 回答