private ArrayList<File> findAllSong(File file) {
File[] files = file.listFiles();
ArrayList<File> songList = new ArrayList<>();
for (File eachSongFile : files) {
if (eachSongFile.isDirectory() && !eachSongFile.isHidden()) {
songList.addAll(findAllSong(eachSongFile));
} else {
if (eachSongFile.getName().endsWith(".mp3") eachSongFile.getName().endsWith(".wav")
) {
songList.add(eachSongFile);
}
}
}
//songList.remove(0);
return songList;
}
Hallo habe das Problem das Ich hier bei files ein NullPointerException fehler habe. Belasse Ich dies so, stürzt das Programm ab. ändere ich dies zuif (files != null) { for (File eachSongFile : files) {öffnet das Programm aber findet keine Lieder.hm... Bitte um Hilfe
hier denn gesamten Mainactivity.java
package com.example.audiomusicplayer;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.FileObserver;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.io.File;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
String songItem[];
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=findViewById(R.id.listViewId);
CheckUserPermsions();
}
void CheckUserPermsions() {
if (Build.VERSION.SDK_INT >= 23) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) !=
PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE},
REQUEST_CODE_ASK_PERMISSIONS);
return;
}
}
display(); // init the Gallary Display
}
//get acces to location permsion
final private int REQUEST_CODE_ASK_PERMISSIONS = 123;
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_PERMISSIONS:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
display(); // // init the Gallary Display
} else {
// Permission Denied
Toast.makeText(this, "permission Denial", Toast.LENGTH_SHORT)
.show();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
private ArrayList<File> findAllSong(File file) {
File[] files = file.listFiles();
ArrayList<File> songList = new ArrayList<>();
for (File eachSongFile : files) {
if (eachSongFile.isDirectory() && !eachSongFile.isHidden()) {
songList.addAll(findAllSong(eachSongFile));
} else {
if (eachSongFile.getName().endsWith(".mp3") eachSongFile.getName().endsWith(".wav")
) {
songList.add(eachSongFile);
}
}
}
//songList.remove(0);
return songList;
}
void display() {
final ArrayList<File> myAllSong = findAllSong(Environment.getExternalStorageDirectory());
songItem = new String[myAllSong.size()];
for (int j = 0; j < myAllSong.size(); j++) {
songItem[j] = myAllSong.get(j).getName().toString()
.replace(".mp3","").replace(".wav","")
;
// toast(myAllSong.get(j).getName().toString());
}
ArrayAdapter<String> arrayAdapter=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,songItem);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String songName=listView.getItemAtPosition(position).toString();
Intent intent=new Intent(MainActivity.this,MyPlayerActivity.class);
intent.putExtra("mySongName",songName);
intent.putExtra("songPos",position);
intent.putExtra("allSong",myAllSong);
startActivity(intent);
}
});
Toast.makeText(this, "Wähle einen Song!", Toast.LENGTH_LONG).show();
}
// public void toast(String text){
// Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
// }
}