草庐IT

android - 显示 SD 卡中的所有歌曲 Genre Wise

coder 2023-12-18 原文

我正在做音乐播放器应用程序。并希望根据其流派显示所有歌曲。如果可能的话,请给我一些提示。我能够显示关于艺术家和专辑的所有歌曲,但在寻找流派明智的歌曲时遇到问题。我的输出是显示每个流派类别中的所有歌曲。它不会根据流派破坏歌曲。我的代码在下面。 LocalGenre.java

package com.PageViewerTilesDemo.src;

import java.util.ArrayList;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Window;
import android.widget.ExpandableListView;
import android.widget.TextView;

public class LocanGenre extends Activity {

    ExpandableListView listLocalArtists;
    TextView txttitle;
    Cursor musiccursor, musiccursor1;
    int music_column_index, music_column_index1;
    int count, count1;

    ArrayList<String> genresName = new ArrayList<String>();
    ArrayList<String> genreID = new ArrayList<String>();
    ArrayList<Integer> albumID = new ArrayList<Integer>();
    ArrayList<String> numberOFSongs = new ArrayList<String>();
    ArrayList<String> artistName = new ArrayList<String>();
    ArrayList<String> path = new ArrayList<String>();
    ArrayList<String> path12 = new ArrayList<String>();
    ArrayList<ArrayList<String>> pathDisplay = new ArrayList<ArrayList<String>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.localartists);

        txttitle = (TextView) findViewById(R.id.title);

        txttitle.setText("Genres");

        listLocalArtists = (ExpandableListView) findViewById(R.id.listView1);

        init_phone_music_grid();

        listLocalArtists.setAdapter(new ExpandableListGenreAdapter(this, path, genresName,
                genresName, pathDisplay,albumID));
    }

    private void init_phone_music_grid() {
        // TODO Auto-generated method stub

        System.gc();

        String[] proj = {
                MediaStore.Audio.Media._ID,
                MediaStore.Audio.Media.DISPLAY_NAME,
                MediaStore.Audio.Media.ALBUM_ID};

        musiccursor1 = managedQuery(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, null, null,
                null);

        count1 = musiccursor1.getCount();

        if (count1 > 0) {
            musiccursor1.moveToFirst();
            do {

                music_column_index1 = musiccursor1
                        .getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);

                String filename0 = musiccursor1.getString(music_column_index1);
                path.add(filename0);

                Log.i("LocalGenres  ", "Path  Main" + path);

                music_column_index1 = musiccursor1
                        .getColumnIndexOrThrow(MediaStore.Audio.Media._ID);

                String filename123 = musiccursor1
                        .getString(music_column_index1);
                path12.add(filename123);

                Log.i("LocalGenre", "Media ID  " + path12);

                music_column_index1 = musiccursor1
                        .getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID);

                int filename1 = musiccursor1.getInt(music_column_index1);


                albumID.add(filename1);
                Log.i("LOCAL Genres!!!", " ALBUM ID" + albumID);


            } while (musiccursor1.moveToNext());
        }

        String[] projection = { MediaStore.Audio.Genres._ID,
                MediaStore.Audio.Genres.NAME};

        musiccursor = managedQuery(
                MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI, projection, null,
                null, null);

        genresName.clear();

        count = musiccursor.getCount();

        if (count > 0) {
            musiccursor.moveToFirst();
            do {

                music_column_index = musiccursor
                        .getColumnIndexOrThrow(MediaStore.Audio.Genres._ID);

                String filename = musiccursor.getString(music_column_index);
                if(!genreID.contains(filename))
                {
                genreID.add(filename);
                }

                Log.i("Local Genres  ", "Genre ID" + genreID);

                music_column_index = musiccursor
                        .getColumnIndexOrThrow(MediaStore.Audio.Genres.NAME);

                String filename1 = musiccursor.getString(music_column_index);
                if(!genresName.contains(filename1))
                {
                genresName.add(filename1);
                }

                Log.i("Local Genres  ", "Genres Name  " + genresName);

                /*
                 * music_column_index = musiccursor
                 * .getColumnIndexOrThrow(MediaStore.Audio.Genres._COUNT);
                 * 
                 * String filename3 = musiccursor.getString(music_column_index);
                 * artistName.add(filename3);
                 * 
                 * Log.i("Local Albums  ", "Album ID for Gen  " + artistName);
                 */

            } while (musiccursor.moveToNext());
        }

        for (int j = 0; j < genreID.size(); j++) {

            ArrayList<String> arr = new ArrayList<String>();

            for (int i = 0; i < path12.size(); i++) {
                Log.i("EEEEEE", "Inside If path12.get(i) :"+path12.get(i));
                Log.i("EEEEEE", "Inside If genreID.get(j) :"+genreID.get(j));
                Log.i("EEEEEE", "Inside If Integer.parseInt(path12.get(i)) :"+Integer.parseInt(path12.get(i)));
                Log.i("EEEEEE", "Inside If j : "+j);
                if (path12.get(i).equalsIgnoreCase(genreID.get(j)) || Integer.parseInt(path12.get(i))>j) {

                    Log.i("EEEEEE", "Inside If");
                    arr.add(path.get(i));
                }

                else
                    Log.i("xxxxxxx", "Inside else");
                arr.add(path.get(i));
            }

            Log.i("EEEEEE", "Inside outerloop " + arr);

            pathDisplay.add(arr);
        }
    }
}

ExpandableListGenreAdapter.java

    package com.PageViewerTilesDemo.src;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ExpandableListGenreAdapter  extends BaseExpandableListAdapter{

    private Context context;

    private ArrayList<String> artist;

    private ArrayList<String> genres;

    private ArrayList<ArrayList<String>> children;

    public ArrayList<String> pathmain ;

    public ArrayList<Integer> genresID;

    public ArrayList<Integer> albumID;

    public ExpandableListGenreAdapter(Context context, ArrayList<String> path, ArrayList<String> groups,ArrayList<String> artist,
            ArrayList<ArrayList<String>> children, ArrayList<Integer> albumID) {
        this.context = context;
        this.genres = groups;
        this.artist = artist;
        this.pathmain = path;
        this.children = children;
        this.albumID=albumID;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return children.get(groupPosition).get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return childPosition;
    }

    /*public Bitmap getAlbumart(int album_id) 
       {
            Bitmap bm = null;
            try 
            {
                final Uri sArtworkUri = Uri
                    .parse("content://media/external/audio/albumart");

                Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);

                ParcelFileDescriptor pfd = context.getContentResolver()
                    .openFileDescriptor(uri, "r");

                if (pfd != null) 
                {
                    FileDescriptor fd = pfd.getFileDescriptor();
                    bm = BitmapFactory.decodeFileDescriptor(fd);
                }
        } catch (Exception e) {
        }
        return bm;
    }*/

    @Override
    public View getChildView(final int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final String vehicle = (String) getChild(groupPosition, childPosition);

        Log.i("ExpandableListAdapter", "Group Position  "+groupPosition);

        Log.i("ExpandableListAdapter", "Vehicle  "+vehicle);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.child_layout, null);
        }

        TextView tv = (TextView) convertView.findViewById(R.id.tvChild);
        ImageView imageview1=(ImageView)convertView.findViewById(R.id.ImageView01);
   //     bm=getAlbumart(albumids.get(1));
       // Log.i("LIST ADAPTER","@@@@@@@@@@@@@@@@@@@ALBUM IDS "+albumids.get(0)+"BITMAPPPPP@@@"+bm);

       // imageview1.setImageBitmap(coverart.get(childPosition));
        tv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                MainActivity.flag = true;

                TestFragment3.flag = true;

                Firstpage.flag = true;

                Log.i("ExpandableListGenreAdapter", "path  "+childPosition);

                MainActivity.currentPosition = groupPosition;

                Log.i("ExpandableListGenreAdapter", "currentPosition  "+MainActivity.currentPosition);

                MainActivity.genre=true;
                MainActivity.currentgenreposition = albumID.get(childPosition);

                Log.i("ExpandableListGenreAdapter", "currentGenrePosition  "+MainActivity.currentgenreposition);

                MainActivity.Media_full_path = "/sdcard/"+vehicle;

                Log.i("ExpandableListAdapter", "Onclick  "+MainActivity.Media_full_path);

                ((Activity)context).finish();


            }
        });

        tv.setText("   " + vehicle.toString());

        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return children.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return genres.get(groupPosition);
    }

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
         String group = (String) getGroup(groupPosition);
            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.group_layout, null);
            }
            TextView txtArtistsName = (TextView) convertView.findViewById(R.id.txtArtistsName);
            TextView txtartistssongs = (TextView) convertView.findViewById(R.id.txtartistssongs);
            txtArtistsName.setText(group);
            txtartistssongs.setText(genres.get(groupPosition)+" Song(s)");
            return convertView;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return true;
    }

}

请建议我上面的代码中我缺少的地方。谢谢。

最佳答案

您好,希望对您有所帮助。它显示流派及其歌曲。

int index;
long genreId;
Uri uri;
Cursor genrecursor;
Cursor tempcursor;
String[] proj1 = {MediaStore.Audio.Genres.NAME, MediaStore.Audio.Genres._ID};     
String[] proj2 = {MediaStore.Audio.Media.DISPLAY_NAME};

genrecursor = managedQuery(MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI, proj1, null, null, null);
if (genrecursor.moveToFirst()) {
  do {
    index = genrecursor.getColumnIndexOrThrow(MediaStore.Audio.Genres.NAME);              
    Log.i("Tag-Genre name", genrecursor.getString(index));

    index = genrecursor.getColumnIndexOrThrow(MediaStore.Audio.Genres._ID);               
    genreId = Long.parseLong(genrecursor.getString(index));
    uri = MediaStore.Audio.Genres.Members.getContentUri("external", genreId);

    tempcursor = managedQuery(uri, proj2, null,null,null);
    Log.i("Tag-Number of songs for this genre", tempcursor.getCount() + "");
    if (tempcursor.moveToFirst()) {
      do {
        index = tempcursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
        Log.i("Tag-Song name", tempcursor.getString(index));
      } while(tempcursor.moveToNext());
    }
  } while(genrecursor.moveToNext());       
}

关于android - 显示 SD 卡中的所有歌曲 Genre Wise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9785370/

有关android - 显示 SD 卡中的所有歌曲 Genre Wise的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. ruby - 其他文件中的 Rake 任务 - 2

    我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时

  3. ruby-on-rails - Ruby net/ldap 模块中的内存泄漏 - 2

    作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代

  4. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  5. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  6. ruby - 如何以所有可能的方式将字符串拆分为长度最多为 3 的连续子字符串? - 2

    我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123

  7. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  8. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  9. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  10. ruby-on-rails - Rails 应用程序中的 Rails : How are you using application_controller. rb 是新手吗? - 2

    刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr

随机推荐