[英]Android Studio music player cant read from sdcard, only internal memory
我很抱歉,如果這是一個愚蠢的問題,它可能會成為一個快速解決方案,但我只是無法弄明白。 我正在android studio中構建一個音樂播放器,並且sdcard上的所有歌曲都沒有顯示在列表視圖中,只有內部存儲器中的那些,即使我確實實現了getExternalStorageDirectory()並在清單文件中添加了權限。 關於這種或建設性批評的任何意見都是非常苛刻的。 這是主要的java類。
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();
}
}
使用此方法從您的設備獲取filePath列表
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();
}
......
}
注意:使用“/ storage / sdcard1 /”從sdCard讀取文件,並使用Environment.getExternalStorageDirectory()。getAbsolutePath()從手機內存中讀取文件。
希望這會幫助你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.