Android Studio 音乐播放器无法从 SD 卡读取,只能从内存读取
Android Studio music player cant read from sdcard, only internal memory
如果这是一个愚蠢的问题,我深表歉意,它可能是一个快速解决方案,但我就是想不通。我正在 android studio 中构建一个音乐播放器,并且 sdcard 上的 none 歌曲没有出现在列表视图中,只有内部存储器中的歌曲,即使我确实实现了 getExternalStorageDirectory() 和在清单文件中添加了权限。
非常感谢对此或建设性批评的任何意见。这里主要是 java class.
public class MainActivity extends AppCompatActivity {
ListView lv;
String[] items;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lvPlayList);
//---------------------------------------------> V HERE V <-------------------------
final ArrayList<File> mySongs = findSongs(Environment.getExternalStorageDirectory());
//----------------------------------------------------------------------------------
items = new String[ mySongs.size() ];
for(int i = 0; i<mySongs.size(); i++) {
toast(mySongs.get(i).getName().toString());
items[i] = mySongs.get(i).getName().toString().replace(".mp3", "").replace(".wav", "");
}
ArrayAdapter<String> adp = new ArrayAdapter<>(getApplicationContext(), R.layout.song_layout, R.id.textView, items);
lv.setAdapter(adp);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
startActivity(new Intent(getApplicationContext(), Player.class).putExtra("pos",position).putExtra("songlist",mySongs));
}
});
}
public ArrayList<File> findSongs(File root) {
ArrayList<File> al = new ArrayList<>();
File[] files = root.listFiles();
for(File singleFile : files) {
if(singleFile.isDirectory() && !singleFile.isHidden()) {
al.addAll(findSongs(singleFile));
} else {
if(singleFile.getName().endsWith(".mp3") || singleFile.getName().endsWith(".wav")) {
al.add(singleFile);
}
}
}
return al;
}
public void toast(String text) {
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
}
}
使用此方法从您的设备获取文件路径列表
ArrayList<String> findSongs(String rootPath) {
ArrayList<String> fileList = new ArrayList<>();
try{
File rootFolder = new File(rootPath);
File[] files = rootFolder.listFiles(); //here you will get NPE if directory doesn't contains any file,handle it like this.
for (File file : files) {
if (file.isDirectory()) {
if (findSongs(file.getAbsolutePath()) != null) {
fileList.addAll(findSongs(file.getAbsolutePath()));
} else {
break;
}
} else if (file.getName().endsWith(".mp3")) {
fileList.add(file.getAbsolutePath());
}
}
return fileList;
}catch(Exception e){
return null;
}
}
但您需要从 sdCard 获取文件,为此将“/storage/sdcard1/”作为 findSongs(String path) 方法的参数,如下所示:
onCreate(Bundle svaeInstance){
........
........
if(findSongs("/storage/sdcard1/")!=null){
ArrayList<String> listOfFilePath=findSongs("/storage/sdcard1/"); // here you will get all the files path which contains .mp3 at the end.
//do the remaining stuff
}else { Toast.makeText(this, "sdCard not available", Toast.LENGTH_SHORT).show();
}
......
}
Note: use "/storage/sdcard1/" for reading files from sdCard and use Environment.getExternalStorageDirectory().getAbsolutePath() for reading files from phone memory.
希望对您有所帮助。
如果这是一个愚蠢的问题,我深表歉意,它可能是一个快速解决方案,但我就是想不通。我正在 android studio 中构建一个音乐播放器,并且 sdcard 上的 none 歌曲没有出现在列表视图中,只有内部存储器中的歌曲,即使我确实实现了 getExternalStorageDirectory() 和在清单文件中添加了权限。 非常感谢对此或建设性批评的任何意见。这里主要是 java class.
public class MainActivity extends AppCompatActivity {
ListView lv;
String[] items;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lvPlayList);
//---------------------------------------------> V HERE V <-------------------------
final ArrayList<File> mySongs = findSongs(Environment.getExternalStorageDirectory());
//----------------------------------------------------------------------------------
items = new String[ mySongs.size() ];
for(int i = 0; i<mySongs.size(); i++) {
toast(mySongs.get(i).getName().toString());
items[i] = mySongs.get(i).getName().toString().replace(".mp3", "").replace(".wav", "");
}
ArrayAdapter<String> adp = new ArrayAdapter<>(getApplicationContext(), R.layout.song_layout, R.id.textView, items);
lv.setAdapter(adp);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
startActivity(new Intent(getApplicationContext(), Player.class).putExtra("pos",position).putExtra("songlist",mySongs));
}
});
}
public ArrayList<File> findSongs(File root) {
ArrayList<File> al = new ArrayList<>();
File[] files = root.listFiles();
for(File singleFile : files) {
if(singleFile.isDirectory() && !singleFile.isHidden()) {
al.addAll(findSongs(singleFile));
} else {
if(singleFile.getName().endsWith(".mp3") || singleFile.getName().endsWith(".wav")) {
al.add(singleFile);
}
}
}
return al;
}
public void toast(String text) {
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
}
}
使用此方法从您的设备获取文件路径列表
ArrayList<String> findSongs(String rootPath) {
ArrayList<String> fileList = new ArrayList<>();
try{
File rootFolder = new File(rootPath);
File[] files = rootFolder.listFiles(); //here you will get NPE if directory doesn't contains any file,handle it like this.
for (File file : files) {
if (file.isDirectory()) {
if (findSongs(file.getAbsolutePath()) != null) {
fileList.addAll(findSongs(file.getAbsolutePath()));
} else {
break;
}
} else if (file.getName().endsWith(".mp3")) {
fileList.add(file.getAbsolutePath());
}
}
return fileList;
}catch(Exception e){
return null;
}
}
但您需要从 sdCard 获取文件,为此将“/storage/sdcard1/”作为 findSongs(String path) 方法的参数,如下所示:
onCreate(Bundle svaeInstance){
........
........
if(findSongs("/storage/sdcard1/")!=null){
ArrayList<String> listOfFilePath=findSongs("/storage/sdcard1/"); // here you will get all the files path which contains .mp3 at the end.
//do the remaining stuff
}else { Toast.makeText(this, "sdCard not available", Toast.LENGTH_SHORT).show();
}
......
}
Note: use "/storage/sdcard1/" for reading files from sdCard and use Environment.getExternalStorageDirectory().getAbsolutePath() for reading files from phone memory.
希望对您有所帮助。