Get the App
SLTechnology News&Howtos  ›  Development  › 

How to read all the music in the memory of the phone by Android

Shulou Source: shulou.com Published: 2022-06-03 02:34:36 09月14日 Update

This article mainly introduces how to read all the music in the memory of the mobile phone by Android, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let's take a look at it.

To read the music information in the phone's memory, you need to do the following steps:

1) create a XML file, store the obtained information, and use the ListView control

2) create a new XML file to lay out the item objects in LIstView

3) you need to create a Music class to store the main information of the Music you need to obtain.

4) you need to create a help class to get the music information on the phone, and pass the information through a collection class.

5) create a custom adapter to load each child Item of ListView

6) load the data and return to the user interface

The main code implementation is as follows:

1. Helper module

Public ArrayList getMusic (Context context) {arrayList = new ArrayList (); resolver = context.getContentResolver (); cursor = resolver.query (MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER); if (cursor! = null) {while (cursor.moveToNext ()) {musicInfo = new MusicInfo (); musicInfo.setSinger_Name (cursor.getString (cursor. GetColumnIndex (MediaStore.Audio.Media.ARTIST) / / singer name musicInfo.setSong_Name (cursor.getString (cursor. GetColumnIndex (MediaStore.Audio.Media.TITLE); / / song name musicInfo.setSong_size (FormatChange.formatSize (cursor.getInt (cursor. GetColumnIndex (MediaStore.Audio.Media.SIZE); / / size of the song musicInfo.setSong_ time (FormatChange.toTime (cursor. GetColumnIndexOrThrow (MediaStore.Audio.Media.DURATION) / / Song time musicInfo.setUrl (cursor.getString (cursor .getColumnIndex (MediaStore.Audio.Media.DATA)); arrayList.add (musicInfo);}} cursor.close (); return arrayList;}

I do it in a normal way, or I can use a static implementation.

II. Adapter module

Private ArrayList arrayList; private Context context; private TextView music_singer,music_name,music_size; private MusicInfo musicInfo; public Music_adapter (Context context,ArrayList arrayList) {this.context=context; this.arrayList=arrayList;} @ Override public int getCount () {return arrayList.size ();} @ Override public Object getItem (int position) {return arrayList.get (position);} @ Override public long getItemId (int id) {return id } @ Override public View getView (int position, View view, ViewGroup arg2) {if (view==null) {view=LayoutInflater.from (context) .comparate (R.layout.musicFormlNull);} music_name= (TextView) view.findViewById (R.id.music_name); music_singer= (TextView) view.findViewById (R.id.music_singer); music_size= (TextView) view.findViewById (R.id.music_size); musicInfo= (MusicInfo) arrayList.get (position) Music_name.setText (musicInfo.getSong_Name ()); music_singer.setText (musicInfo.getSinger_Name () + "|" + musicInfo.getSong_time ()); music_size.setText (musicInfo.getSong_size ()); / / you must convert return view to string type here. } Thank you for reading this article carefully. I hope the article "how to read all the music in the memory of your mobile phone" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Information mobile phone music article memory song help file module adapter adaptation normal size code value interest at the same time size character string Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Technology Redmi Huawei Shulou Information