How to get the No of track from an Artist in mediastore

断了今生、忘了曾经 提交于 2019-12-25 18:02:59

问题


I am trying to get the no of track in Artist table using mediastore. I am able to get all the artist name but when I try to get the NUMBER_OF_TRACKS I am getting error .I searched and found this But this is not the problem in my case .Here is my code

 try {
        String[] proj = {MediaStore.Audio.Artists._ID,
                MediaStore.Audio.Artists.ARTIST,
                MediaStore.Audio.ArtistsColumns.NUMBER_OF_TRACKS,
                MediaStore.Audio.Artists.ARTIST_KEY
                ,MediaStore.Audio.Artists.NUMBER_OF_ALBUMS
        };// Can include more data for more details and check it.

        String selection = null;

        String[] selectionArgs = null;

        String sortOrder = MediaStore.Audio.Artists.ARTIST + " ASC";

        Cursor audioCursor = getContentResolver().query(MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI, proj, selection, selectionArgs, sortOrder);

        if (audioCursor != null) {
            if (audioCursor.moveToFirst()) {
                do {
                    int artistname = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Artists.ARTIST);
                    int artist_id = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Artists._ID);
                    int artist_track = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Artists.NUMBER_OF_TRACKS);
                    int artist_key = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Artists.ARTIST_KEY);
                    Mediafileinfo info = new Mediafileinfo();
                    info.setArtistname(audioCursor.getString(artistname));
                    info.setArtistkey(audioCursor.getString(artist_key));
                    info.setArtist_track(audioCursor.getInt(artist_track));
                    info.setArtist_id(audioCursor.getString(artist_id));
                    audioList.add(info);
                } while (audioCursor.moveToNext());

and the adapter code is

 Mediafileinfo item = (Mediafileinfo) getItem(position);
    TextView artist = (TextView) view.findViewById(R.id.textView);
    TextView count = (TextView) view.findViewById(R.id.textView2);

    artist.setText(item.artistname);
    count.setText(item.artist_track);

What I am doing wrong? And how to use No of Track in mediastore any example when I try I am getting error like this

 FATAL EXCEPTION: main
              android.content.res.Resources$NotFoundException: String resource ID #0x1
                  at android.content.res.Resources.getText(Resources.java:233)
                  at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
                  at android.widget.TextView.setText(TextView.java:3657)
                  at com.example.neelay.level10.CustomAdapter.getView(CustomAdapter.java:35)
                  at android.widget.AbsListView.obtainView(AbsListView.java:2267)
                  at android.widget.ListView.makeAndAddView(ListView.java:1769)
                  at android.widget.ListView.fillDown(ListView.java:672)
                  at android.widget.ListView.fillFromTop(ListView.java:733)
                  at android.widget.ListView.layoutChildren(ListView.java:1622)
                  at android.widget.AbsListView.onLayout(AbsListView.java:2102)
                  at android.view.View.layout(View.java:13902)
                  at android.view.ViewGroup.layout(ViewGroup.java:4476)
                  at android.widget.RelativeLayout.onLayout(RelativeLayout.java:953)
                  at android.view.View.layout(View.java:13902)
                  at android.view.ViewGroup.layout(ViewGroup.java:4476)
                  at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1139)
                  at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:833)
                  at android.support.design.widget.ViewOffsetBehavior.layoutChild(ViewOffsetBehavior.java:63)
                  at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:135)
                  at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
                  at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1364)
                  at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:846)
                  at android.view.View.layout(View.java:13902)
                  at android.view.ViewGroup.layout(ViewGroup.java:4476)
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:463)
                  at android.view.View.layout(View.java:13902)
                  at android.view.ViewGroup.layout(ViewGroup.java:4476)
                  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1730)
                  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1588)
                  at android.widget.LinearLayout.onLayout(LinearLayout.java:1487)
                  at android.view.View.layout(View.java:13902)
                  at android.view.ViewGroup.layout(ViewGroup.java:4476)
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:463)
                  at android.view.View.layout(View.java:13902)
                  at android.view.ViewGroup.layout(ViewGroup.java:4476)
                  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1730)
                  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1588)
                  at android.widget.LinearLayout.onLayout(LinearLayout.java:1487)
                  at android.view.View.layout(View.java:13902)
                  at android.view.ViewGroup.layout(ViewGroup.java:4476)
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:463)
                  at android.view.View.layout(View.java:13902)
                  at android.view.ViewGroup.layout(ViewGroup.java:4476)
                  at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1876)
                  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1697)
                  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1008)
                  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4222)
                  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
                  at android.view.Choreographer.doCallbacks(Choreographer.java:555)
                  at android.view.Choreographer.doFrame(Choreographer.java:525)
                  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
                  at android.os.Handler.handleCallback(Handler.java:615)
                  at android.os.Handler.dispatchMessage(Handler.java:92)
                  at android.os.Looper.loop(Looper.java:137)
                  at android.app.ActivityThread.main(ActivityThread.java:4793)
                  at java.lang.reflect.Method.invokeNative(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:511)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:808)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
                  at dalvik.system.NativeStart.main(Native Method)

回答1:


I had a look at the database itself using Razorsql on a pc. The data is held in a view called artist_info. The required detail is there. Instead of

 MediaStore.Audio.Artists.NUMBER_OF_TRACKS);

try

 MediaStore.Audio.ArtistColumns.NUMBER_OF_TRACKS;

Found this example:

                    MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI, new String[] {

        BaseColumns._ID,

        ArtistColumns.ARTIST,

        ArtistColumns.NUMBER_OF_ALBUMS,

        ArtistColumns.NUMBER_OF_TRACKS }, null, null,null);


来源:https://stackoverflow.com/questions/40000217/how-to-get-the-no-of-track-from-an-artist-in-mediastore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!