Android Show All Mp3 Media Files From SD Card in ListView

June 05, 2018 , , 0 Comments

This tutorial is very useful for developers who wish to create Mp3 media files player applications. So in this tutorial we would going to create a android application to Show All Mp3 Media Files From SD Card in custom ListView on button click using Cursor control. We would store one by one all the MP3 file names inside String Array and then set the array into Array adapter. We would also request the runtime permission for READ_EXTERNAL_STORAGE. So let’s get started 🙂 .


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


Note : You need to manually Add  permission to AndroidManifest.xml file.


Code for MainActivity.java file.


package com.android_examples.getallaudiosdcard_android_examplescom;
import android.Manifest;
import android.provider.MediaStore;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.net.Uri;
import android.os.Build;
import android.content.Context;
import android.content.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.List;

public class MainActivity extends AppCompatActivity {

Context context;

public static final int RUNTIME_PERMISSION_CODE = 7;

String[] ListElements = new String[] { };

ListView listView;

List ListElementsArrayList ;

ArrayAdapter adapter ;

ContentResolver contentResolver;

Cursor cursor;

Uri uri;

Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.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.layout.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 item selected listener.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {

// TODO Auto-generated method stub
// Showing ListView Item Click Value using Toast.

Toast.makeText(MainActivity.this,parent.getAdapter().getItem(position).toString(),Toast.LENGTH_LONG).show();

}
});

}


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 Wrong.", 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 Song ID From Cursor.
//int id = cursor.getColumnIndex(MediaStore.Audio.Media._ID);

do {

// You can also get the Song ID using cursor.getLong(id).
//long SongID = cursor.getLong(id);

String SongTitle = cursor.getString(Title);

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

} while (cursor.moveToNext());
}
}

// Creating Runtime permission function.
public void AndroidRuntimePermission(){

if(Build.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){

switch(requestCode){

case RUNTIME_PERMISSION_CODE:{

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

}
else {

}
}
}
}
}

Code for activity_main.xml layout file.



xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.android_examples.getallaudiosdcard_android_examplescom.MainActivity">


android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_margin="3dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Click Here To Show All MP3 Files in ListView"
android:layout_marginTop="10dp"
android:background="#009688"
android:textColor="#fff"/>

android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/button" />


Code for AndroidManifest.xml file.



package="com.android_examples.getallaudiosdcard_android_examplescom">



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/AppTheme">










Screenshots :


Show All Mp3 Media Files


The post Android Show All Mp3 Media Files From SD Card in ListView appeared first on Android Examples.

siddharth makadiya

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google

0 comments:

Popular Posts