我将jid3lib用于Java.当我使用此库中存储在计算机中的文件来工作时,它很好用,但是当我尝试在模拟器的sdcard上读取文件的mp3标签时,我得到了Exeption.
我使用以下代码:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); File musicPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC); File[] songs = musicPath.listFiles(); for (int i=0; i < songs.length; i++){ try { MP3File song = new MP3File(new File(songs[i].getPath())); Log.d("TEST", song.getID3v1Tag().getAlbum() ); } catch (TagException e) { Log.d("TEST", "Tag Exception"); e.printStackTrace(); } catch (Exception e) { Log.d("TEST", "Exception"); e.printStackTrace(); } } }我在sdcard中放入了2个mp3文件,但在日志中仍然出现异常. 这可能是什么问题?
我在LogCat上遇到以下错误:
java.io.FileNotFoundException: /mnt/sdcard/Music/song.mp3: open failed: EACCES (Permission denied)但是,当我在DDMS的文件资源管理器中查看时,即使在使用循环来打印文件名的情况下,也可以看到该路径下的文件.
解决方案
EACCES(权限被拒绝)
在清单文件中添加 READ_EXTERNAL_STORAGE 权限. /p>
而且,也许WRITE_EXTERNAL_STORAGE也是如此.
I use jid3lib for java. When i'm working with this library on file that stored in my computer it works great, but when I try to read mp3 tags of files on the emulator's sdcard I got Exeption.
I use the following code:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); File musicPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC); File[] songs = musicPath.listFiles(); for (int i=0; i < songs.length; i++){ try { MP3File song = new MP3File(new File(songs[i].getPath())); Log.d("TEST", song.getID3v1Tag().getAlbum() ); } catch (TagException e) { Log.d("TEST", "Tag Exception"); e.printStackTrace(); } catch (Exception e) { Log.d("TEST", "Exception"); e.printStackTrace(); } } }I put 2 mp3 files in the sdcard, but I still got Exception in the Log. What can be the problem here?
I got the following error at the LogCat:
java.io.FileNotFoundException: /mnt/sdcard/Music/song.mp3: open failed: EACCES (Permission denied)But when I'm looking in the File Explorer of the DDMS I can see the files under this path, and even if I use a loop in order to print the file's names It show the correct names.
解决方案EACCES (Permission denied)
Add READ_EXTERNAL_STORAGE permission in manifest file.
And, maybe WRITE_EXTERNAL_STORAGE also.
更多推荐
在Android应用程序中读取mp3标签
发布评论