my problem is that I want to load all music file format but also I have created music list but when am run app it do not show the music list from the internal storage the following are the codes
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private static final String[] PERMISSIONS = {
Manifest.permission.READ_EXTERNAL_STORAGE
};
//request permission
private static final int REQUEST_PERMISSION = 12345;
//store counted permission
private static final int PERMISSION_COUNT = 1;
//check for permission asked
@SuppressLint("NewApi")
private boolean arePermissionDenied(){
for (int i =0; i < PERMISSION_COUNT; i++){
//check if permission granted
if (checkSelfPermission(PERMISSIONS[i])!= PackageManager.PERMISSION_GRANTED){
return true;
}
}
return false;
}
//get the result of the permission
@SuppressLint("NewApi")
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[]grantResults){
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//if permission denied close the app
if (arePermissionDenied()){
((ActivityManager) (this.getSystemService(ACTIVITY_SERVICE))).clearApplicationUserData();
recreate();
}
else{
onResume();
}
}
//initializing music player
private boolean isMusicPlayerInit;
//create variable to store music file
private List<String> musicFilesList;
//add music from the directory
private void addMusicFrom(String dirPath){
final File musicDir = new File(dirPath);
if (!musicDir.exists()){
musicDir.mkdir();
return;
}
final File[] files = musicDir.listFiles();
for (File file : files){
final String path = file.getAbsolutePath();
if (path.endsWith(".mp3")){
musicFilesList.add(path);
}
}
}
private void fillMusicList(){
musicFilesList.clear();
addMusicFrom(String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)));
addMusicFrom(String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)));
}
private void playMusicFiles(String path){
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(path);
mp.prepare();
mp.start();
}catch (Exception e){
e.printStackTrace();
}
}
@Override
protected void onResume(){
super.onResume();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && arePermissionDenied()){
requestPermissions(PERMISSIONS, REQUEST_PERMISSION);
return;
}
if (!isMusicPlayerInit){
final ListView listView = findViewById(R.id.listview);
final TextAdapter textAdapter = new TextAdapter();
musicFilesList = new ArrayList<>();
fillMusicList();
textAdapter.setData(musicFilesList);
listView.setAdapter(textAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final String musicFilePath = musicFilesList.get(position);
playMusicFiles(musicFilePath);
}
});
isMusicPlayerInit = true;
}
}
//create list to hold data
class TextAdapter extends BaseAdapter{
private List<String> data = new ArrayList<>();
void setData(List<String> mData){
data.clear();
data.addAll(mData);
notifyDataSetChanged();
}
@Override
public int getCount(){
return data.size();
}
@Override
public String getItem(int position){
return null;
}
@Override
public long getItemId(int position){
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
if (convertView == null){
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
convertView.setTag(new ViewHolder((TextView) convertView.findViewById(R.id.myItem)) );
}
ViewHolder holder = (ViewHolder) convertView.getTag();
final String item = data.get(position);
holder.info.setText(item.substring((item.lastIndexOf('/')+1)));
return convertView;
}
class ViewHolder{
TextView info;
ViewHolder(TextView mInfo){
info = mInfo;
}
}
}
}
I need how to do that when am open this app no any music is shown from the device and that means no music loaded so how do I load the music from the device all prerequisites like permission I had provided them