Full tutorial is given below. Dear Viewer we are working hard to maintain this website. We doesn't want any think from you. Its just a humble request If you thinks that this posts helps you please share this post with your friends.
Display audio files in ListView
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical" tools:layout_editor_absoluteY="81dp"> <ListView android:id="@+id/audioListView" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </LinearLayout>
MainActivity.java
package com.usmtip.myapplication; import android.database.Cursor; import android.os.Bundle; import android.provider.MediaStore; import android.widget.ArrayAdapter; import android.widget.ListView; import androidx.appcompat.app.AppCompatActivity; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView mAudioListView = (ListView) findViewById(R.id.audioListView); ArrayList<String> mAudioList = new ArrayList<>(); //detail of each audio String[] mAudioDetailArray = { MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DISPLAY_NAME}; //INTERNAL_CONTENT_URI to display audio from internal storage //EXTERNAL_CONTENT_URI to display audio from external storage Cursor mAudioCursor = getContentResolver().query(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, mAudioDetailArray, null, null, null); if(mAudioCursor != null){ if(mAudioCursor.moveToFirst()){ do{ int audioIndex = mAudioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME); mAudioList.add(mAudioCursor.getString(audioIndex)); }while(mAudioCursor.moveToNext()); } } mAudioCursor.close(); ArrayAdapter<String> mAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,android.R.id.text1, mAudioList); mAudioListView.setAdapter(mAdapter); } }
0 Comments: