step 1:First of all create a project in Android studio And as soon as you create the project, give us the file will appear activity_main and MainActivty.
Step 2: First we will use a ListView in activity_main the code is given below
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"/>
</RelativeLayout>
In this layout we have used a ListView through which we will show the song in the listView.Now we will use this layout in MainActivity.MainActivity.java
package com.example.listsong;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ListView listView;
public static final int RUNTIME_PERMISSION=10;
ArrayList<String>arrayList;
ArrayAdapter<String>adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED)
{
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,Manifest.permission.READ_EXTERNAL_STORAGE))
{
ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},RUNTIME_PERMISSION);
}
else
{
ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},RUNTIME_PERMISSION);
}
}
else
{
showSong();
}
}
private void showSong () {
listView=(ListView) findViewById(R.id.listView);
arrayList=new ArrayList<>();
getMusic();
adapter=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,arrayList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
});
}
public void getMusic()
{
ContentResolver contentResolver=getContentResolver();
Uri songUri= MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor songCursor= null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
songCursor = contentResolver.query(songUri,null,null,null);
}
if(songCursor!=null && songCursor.moveToFirst()) {
int songTitle=songCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
int songArtist=songCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
do{
String currentTitle=songCursor.getString(songTitle);
String currentArtist=songCursor.getString(songArtist);
arrayList.add(currentTitle + "\n" + currentArtist);
}while (songCursor.moveToNext());
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case RUNTIME_PERMISSION:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission Granted ...", Toast.LENGTH_SHORT).show();
showSong();
}
} else {
Toast.makeText(this, "Permission Granted ....", Toast.LENGTH_SHORT).show();
finish();
}
}
return;
}
}
In this class we will first take storage permission And we will handle this storage permission through an override method onRequestPermissionsResult in which we will check whether the storage permission is there or not, if the permission is granted then we will call showsong methodthen by calling songShow method we will print all the songs.Inside the showsong method, we have used another method getMusic by which we can print all the tile descriptions of all the songs in the song.through some already made method.like ContentResolver, Cursor,Uri etc.
After doing this, we have to do one more thing in the manifest, we will have to give a permission of "android.permission.READ_EXTERNAL_STORAGE" so that it can take storage permission.
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.listsong">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ListSong">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Thank you
0 Comments
Please do not enter any spam link in the comment box
Emoji