在Android应用程序中读取mp3标签 [英] Read mp3 tags in android application

查看:109
本文介绍了在Android应用程序中读取mp3标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将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标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆