Android Examples Tutorials

Android Show All Mp3 Media Files From SD Card in ListView

This tutorial could be very useful for developers who wish to create Mp3 media information player purposes. So in this tutorial we might going to create a android software to Show All Mp3 Media Files From SD Card in custom ListView on button click on utilizing Cursor management. We might store one by one all of the MP3 file names inside String Array and then set the array into Array adapter. We might also request the runtime permission for READ_EXTERNAL_STORAGE. So let’s get began 🙂 .

The best way to Show All Mp3 Media Files From SD Card in ListView in Android on Button click :

Word : You should manually Add permission to AndroidManifest.xml file.

Code for MainActivity.java file.

package deal com.android_examples.getallaudiosdcard_android_examplescom;
import android.Manifest;
import android.provider.MediaStore;
import android.app.AlertDialog;
import android.content material.ContentResolver;
import android.content material.pm.PackageManager;
import android.help.v4.app.ActivityCompat;
import android.help.v7.app.AppCompatActivity;
import android.internet.Uri;
import android.os.Construct;
import android.content.Context;
import android.content material.DialogInterface;
import android.os.Bundle;
import android.database.Cursor;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Listing;

public class MainActivity extends AppCompatActivity

Context context;

public static remaining int RUNTIME_PERMISSION_CODE = 7;

String[] ListElements = new String[] ;

ListView listView;

Listing ListElementsArrayList ;

ArrayAdapter adapter ;

ContentResolver contentResolver;

Cursor cursor;

Uri uri;

Button button;

@Override
protected void onCreate(Bundle savedInstanceState)

tremendous.onCreate(savedInstanceState);

setContentView(R.format.activity_main);

listView = (ListView) findViewById(R.id.listView1);

button = (Button)findViewById(R.id.button);

context = getApplicationContext();

ListElementsArrayList = new ArrayList<>(Arrays.asList(ListElements));

adapter = new ArrayAdapter
(MainActivity.this, android.R.format.simple_list_item_1, ListElementsArrayList);

// Requesting run time permission for Read External Storage.
AndroidRuntimePermission();

button.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)

GetAllMediaMp3Files();

listView.setAdapter(adapter);

);

// ListView on merchandise chosen listener.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()

@Override
public void onItemClick(AdapterView mother or father, View view, int place, long id)

// TODO Auto-generated technique stub
// Displaying ListView Merchandise Click on Worth using Toast.

Toast.makeText(MainActivity.this,mum or dad.getAdapter().getItem(position).toString(),Toast.LENGTH_LONG).present();

);

public void GetAllMediaMp3Files()

contentResolver = context.getContentResolver();

uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

cursor = contentResolver.query(
uri, // Uri
null,
null,
null,
null
);

if (cursor == null)

Toast.makeText(MainActivity.this,”Something Went Incorrect.”, Toast.LENGTH_LONG);

else if (!cursor.moveToFirst())

Toast.makeText(MainActivity.this,”No Music Found on SD Card.”, Toast.LENGTH_LONG);

else

int Title = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE);

//Getting Track ID From Cursor.
//int id = cursor.getColumnIndex(MediaStore.Audio.Media._ID);

do

// It’s also possible to get the Music ID using cursor.getLong(id).
//long SongID = cursor.getLong(id);

String SongTitle = cursor.getString(Title);

// Including Media File Names to ListElementsArrayList.
ListElementsArrayList.add(SongTitle);

whereas (cursor.moveToNext());

// Creating Runtime permission perform.
public void AndroidRuntimePermission()

if(Construct.VERSION.SDK_INT>=Build.VERSION_CODES.M)

if(checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED)

if(shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE))

AlertDialog.Builder alert_builder = new AlertDialog.Builder(MainActivity.this);
alert_builder.setMessage(“External Storage Permission is Required.”);
alert_builder.setTitle(“Please Grant Permission.”);
alert_builder.setPositiveButton(“OK”, new DialogInterface.OnClickListener()

@Override
public void onClick(DialogInterface dialogInterface, int i)

ActivityCompat.requestPermissions(
MainActivity.this,
new String[]Manifest.permission.READ_EXTERNAL_STORAGE,
RUNTIME_PERMISSION_CODE

);

);

alert_builder.setNeutralButton(“Cancel”,null);

AlertDialog dialog = alert_builder.create();

dialog.show();

else

ActivityCompat.requestPermissions(
MainActivity.this,
new String[]Manifest.permission.READ_EXTERNAL_STORAGE,
RUNTIME_PERMISSION_CODE
);

else

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)

change(requestCode)

case RUNTIME_PERMISSION_CODE:

if(grantResults.length > zero && grantResults[0] == PackageManager.PERMISSION_GRANTED)

else

Code for activity_main.xml format file.