本文介绍了Android Studio 音乐播放器无法从 sdcard 中读取,只能从内存中读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 如果这被证明是一个愚蠢的问题,我深表歉意,这可能是一个快速解决方案,但我就是想不通.我正在 android studio 中构建一个音乐播放器,sdcard 上的所有歌曲都没有显示在列表视图中,只有内存中的歌曲,即使我确实实现了 getExternalStorageDirectory() 并在清单文件中添加了权限.非常感谢对此或建设性批评的任何投入.这是主要的java类.公共类 MainActivity 扩展 AppCompatActivity {ListView lv;字符串[] 项;@覆盖protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);lv = (ListView) findViewById(R.id.lvPlayList);//--------------------------------------------->V 这里 V 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", "");}ArrayAdapteradp = new ArrayAdapter<>(getApplicationContext(), R.layout.song_layout, R.id.textView, items);lv.setAdapter(adp);lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {@覆盖public void onItemClick(AdapterView<?> parent, View view, int position, long id) {startActivity(new Intent(getApplicationContext(), Player.class).putExtra("pos",position).putExtra("songlist",mySongs));}});}公共 ArrayListfindSongs(文件根){ArrayListal = new ArrayList();File[] 文件 = root.listFiles();for(文件单文件:文件){if(singleFile.isDirectory() && !singleFile.isHidden()) {al.addAll(findSongs(singleFile));} 别的 {if(singleFile.getName().endsWith(".mp3") || singleFile.getName().endsWith(".wav")) {al.add(singleFile);}}}返回 al;}公共无效吐司(字符串文本){Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();}} 解决方案 使用此方法从您的设备获取 filePath 列表 ArrayListfindSongs(String rootPath) {ArrayListfileList = new ArrayList();尝试{文件根文件夹 = 新文件(根路径);File[] 文件 = rootFolder.listFiles();//如果目录不包含任何文件,您将在这里获得NPE,像这样处理它.对于(文件文件:文件){如果(文件.isDirectory()){如果 (findSongs(file.getAbsolutePath()) != null) {fileList.addAll(findSongs(file.getAbsolutePath()));} 别的 {休息;}} else if (file.getName().endsWith(".mp3")) {fileList.add(file.getAbsolutePath());}}返回文件列表;}catch(异常e){返回空;}}但是您需要从 sdCard 获取文件,为此传递/storage/sdcard1/"作为 findSongs(String path) 方法的参数,如下所示:onCreate(Bundle svaeInstance){……………………if(findSongs("/storage/sdcard1/")!=null){ArrayListlistOfFilePath=findSongs("/storage/sdcard1/");//在这里你会得到所有包含 .mp3 的文件路径.//做剩下的事情}else { Toast.makeText(this, "sdCard not available", Toast.LENGTH_SHORT).show();}......}注意:使用/storage/sdcard1/"从sdCard读取文件,使用Environment.getExternalStorageDirectory().getAbsolutePath()从手机内存读取文件.希望对你有帮助.I apologize if this turns out to be a stupid question, it might turn out as a quick fix but I just cant figure it out. I'm building a music player in android studio and none of the songs on the sdcard dont show up in the list view, only the ones in internal memory, even though I did implement getExternalStorageDirectory() and added the permission in the manifest file.Any input on this or constructive criticism is greatly apreciated. Here is the main 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(); }} 解决方案 Use this method to get the list of filePath from your device 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; }}but you need to get files from sdCard, for that pass "/storage/sdcard1/" as the parameter for findSongs(String path) method like this: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.Hope this will help you. 这篇关于Android Studio 音乐播放器无法从 sdcard 中读取,只能从内存中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-03 12:27