简体   繁体   English

album_info不包含album_id列错误

[英]album_info does not contain album_id column error

I'm creating a music player that populates an array list with song objects. 我正在创建一个音乐播放器,用歌曲对象填充数组列表。 Each song object has a series of attributes. 每个歌曲对象都有一系列属性。 One of those attributes is the album-art URI. 其中一个属性是专辑艺术URI。

This is the line of code where I assign the URI attribute to the song object. 这是我将URI属性分配给歌曲对象的代码行。

songObject.albumArtURI = GetAlbumArtURI(albumID); 

Here is the string value of albumID 这是albumID的字符串值

Log.v("TAG",String.valueOf(albumID)); // prints [Ljava.lang.String;@44ce53d 

When I pass albumID to GetAlbumArtURI() method 当我将albumID传递给GetAlbumArtURI()方法时

private String GetAlbumArtURI(String[] albumID){

    final Cursor mCursor = getContentResolver().query(
            MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, 
            new String[] {MediaStore.Audio.Albums.ALBUM_ART}, 
            MediaStore.Audio.Albums.ALBUM_ID + "=?", 
            albumID, // Error 
            null
    );

    return mCursor.getString(0);

}

I get this error: 我收到此错误:

no such column: album_id (code 1)
while compiling: 
SELECT album_art FROM album_info WHERE (album_id=?)

The error essentially says that table album_info does not contain album_id column. 该错误实质上表示table album_info不包含album_id列。 But according to the documenation, album_info does have such a column. 但根据该文件, album_info确实有这样一个专栏。

So there's a few issues causing your query to return nothing and throw that error. 因此,有一些问题导致您的查询没有返回任何内容并抛出该错误。

  1. MediaStore.Audio.Albums.ALBUM_ID is not the column you want to reference. MediaStore.Audio.Albums.ALBUM_ID不是您要引用的列。 You should be using MediaStore.Audio.Albums._ID . 您应该使用MediaStore.Audio.Albums._ID

  2. You need to move your cursor's read position to the first position when you get results, if possible. 如果可能,您需要在获得结果时将光标的读取位置移动到第一个位置。 Doing otherwise will result in you never getting the results you need 否则将导致您永远无法获得所需的结果

  3. The way that MediaStore works on android is that you have to register the media files that you want the OS to know about - this isn't automatic. MediaStore在android上运行的方式是你必须注册你希望操作系统知道的媒体文件 - 这不是自动的。 You need to implement something similar to the SingleMediaScanner described in this thread 您需要实现类似于此线程中描述SingleMediaScanner的内容

Here is the working bit of code that I have written: 这是我编写的代码的工作位:

try {
        final Cursor mCursor = getContentResolver().query(
                MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
                new String[] {MediaStore.Audio.Albums.ALBUM_ART},
                MediaStore.Audio.Albums._ID + "=?",
                null,
                null
        );

        // You need to check if there are results in your cursor. 
        // This is like saying if(mCursor.getCount() > 0)
        if(mCursor.moveToFirst()) {
            return mCursor.getString(mCursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
        } else {
            return "";
        }
    } catch (Exception e) {
        Log.e(this.getClass().getSimpleName(),e.getMessage());
    } 

When you have called that code, you're assuming that the MediaStore on your device knows about the music files you've downloaded or added. 当您调用该代码时,您假设设备上的MediaStore知道您已下载或添加的音乐文件。 You can definitely implement a BroadcastReceiver to capture system events like files being added, but for this answer I'm just going to show how you account for one known file. 你绝对可以实现一个BroadcastReceiver来捕获系统事件,比如正在添加的文件,但是对于这个答案,我只是要说明你如何考虑一个已知文件。 You could also expand this to search an entire directory by adding to the onMediaScannerConnected(...) method. 您还可以通过添加到onMediaScannerConnected(...)方法来扩展它以搜索整个目录。

If you implement the SingleMediaScanner file found here you can then just do: 如果您实现此处的SingleMediaScanner文件,则可以执行以下操作:

    File file = new File(Environment.getExternalStorageDirectory() + "/Download/1.mp3");
    SingleMediaScanner singleMediaScanner = new SingleMediaScanner(this, file);

And it will register the media file in your MediaStore. 它将在您的MediaStore中注册媒体文件。 At that point, you should be able to get results back from your query above. 此时,您应该能够从上面的查询中获得结果。 If you are having doubts of whether or not the songs are being registered, you can check to see if any records have been added at all by changing your mCursor call to this (to get all the results in the media store) and then iterating through them: 如果您怀疑这些歌曲是否正在注册,您可以通过更改mCursor对此的调用(以获取媒体商店中的所有结果)来检查是否已添加任何记录,然后通过迭代他们:

 final Cursor mCursor = getContentResolver().query(
                MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
                null,
                null,
                null,
                null
        );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM