Android using jaudiotagger

I try to use jaudiotagger like this but it crashes

Main app.java : import java.io.File; import java.io.IOException;

import org.jaudiotagger.audio.AudioFile;
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.AudioHeader;
import org.jaudiotagger.audio.exceptions.CannotReadException;
import org.jaudiotagger.audio.exceptions.CannotWriteException;
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.tag.FieldDataInvalidException;
import org.jaudiotagger.tag.FieldKey;
import org.jaudiotagger.tag.KeyNotFoundException;
import org.jaudiotagger.tag.Tag;
import org.jaudiotagger.tag.TagException;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class App extends Activity {
    /** Called when the activity is first created. */


    private TextView txt1;
    private TextView txt2;
    private TextView txt3;
    private TextView txt4;
    private TextView txt5;
    private TextView txt6;
    private TextView txt7;
    private TextView txt8;
    private TextView txt9;






    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

       // try
        //{
        File mp3 = new File("/sdcard/test.mp3");
        AudioFile f = null;
        try {
            f = AudioFileIO.read(mp3);
        } catch (CannotReadException e) {
            // TODO Auto-generated catch block
            txt1.setText(e.toString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            txt1.setText(e.toString());
        } catch (TagException e) {
            // TODO Auto-generated catch block
            txt1.setText(e.toString());
        } catch (ReadOnlyFileException e) {
            // TODO Auto-generated catch block
            txt1.setText(e.toString());
        } catch (InvalidAudioFrameException e) {
            // TODO Auto-generated catch block
            txt1.setText(e.toString());
        }
        Tag tag = f.getTag();
        AudioHeader AudioHeader = f.getAudioHeader();
        txt1.setText(tag.getFirst(FieldKey.ARTIST));
        txt2.setText(tag.getFirst(FieldKey.ALBUM));
        txt3.setText(tag.getFirst(FieldKey.TITLE));
        txt4.setText(tag.getFirst(FieldKey.COMMENT));
        txt5.setText(tag.getFirst(FieldKey.YEAR));
        txt6.setText(tag.getFirst(FieldKey.TRACK));
        txt7.setText(tag.getFirst(FieldKey.DISC_NO));
        txt8.setText(tag.getFirst(FieldKey.COMPOSER));
        txt9.setText(tag.getFirst(FieldKey.ARTIST_SORT));




        try {
            tag.setField(FieldKey.ARTIST,"Kings of Leon");
        } catch (KeyNotFoundException e) {
            // TODO Auto-generated catch block
            txt1.setText(e.toString());
        } catch (FieldDataInvalidException e) {
            // TODO Auto-generated catch block
            txt1.setText(e.toString());
        }
        try {
            AudioFileIO.write(f);
        } catch (CannotWriteException e) {
            txt1.setText(e.toString());
        }
       /* }
        catch(Exception x)
        {
            txt1.setText(x.toString());
        }
        */




    }
}

Logcat :

02-22 21:12:22.546: E/AndroidRuntime(19738): FATAL EXCEPTION: main 02-22 21:12:22.546: E/AndroidRuntime(19738): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mp3.tag.editor.alexander.fuchs/com.mp3.tag.editor.alexander.fuchs.App}: java.lang.NullPointerException 02-22 21:12:22.546: E/AndroidRuntime(19738): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) 02-22 21:12:22.546: E/AndroidRuntime(19738): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 02-22 21:12:22.546: E/AndroidRuntime(19738): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 02-22 21:12:22.546: E/AndroidRuntime(19738): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 02-22 21:12:22.546: E/AndroidRuntime(19738): at android.os.Handler.dispatchMessage(Handler.java:99) 02-22 21:12:22.546: E/AndroidRuntime(19738): at android.os.Looper.loop(Looper.java:130) 02-22 21:12:22.546: E/AndroidRuntime(19738): at andro id.app.ActivityThread.main(ActivityThread.java:3691) 02-22 21:12:22.546: E/AndroidRuntime(19738): at java.lang.reflect.Method.invokeNative(Native Method) 02-22 21:12:22.546: E/AndroidRuntime(19738): at java.lang.reflect.Method.invoke(Method.java:507) 02-22 21:12:22.546: E/AndroidRuntime(19738): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907) 02-22 21:12:22.546: E/AndroidRuntime(19738): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665) 02-22 21:12:22.546: E/AndroidRuntime(19738): at dalvik.system.NativeStart.main(Native Method) 02-22 21:12:22.546: E/AndroidRuntime(19738): Caused by: java.lang.NullPointerException 02-22 21:12:22.546: E/AndroidRuntime(19738): at com.mp3.tag.editor.alexander.fuchs.App.onCreate(App.java:72) 02-22 21:12:22.546: E/AndroidRuntime(19738): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 02-22 21:12:22.546: E/AndroidRuntime(19738): at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:1615) 02-22 21:12:22.546: E/AndroidRuntime(19738): ... 11 more


Seems like this line causes the crash, because f is null:

Tag tag = f.getTag();

You shouldn't ignore the exception in the way you do, as if you got an exception, you just print something and continue with a bad state (in this case - f is still null)


There two blockers to using jaudiotagger on Android:

1 - javax.swing

2 - javax.imageio

these two Classes isn't supported by android and jaudiotagger uses them

To solve your problem: Fix the sources so they don't depend no more on these two JAVAX Classes


在获取标签之前,您仍然需要检查f!= null。

Tag r_tag = null;
File testFile = new File(filename);
MP3File f = null;
try {
    f = (MP3File)AudioFileIO.read(testFile);
} catch (CannotReadException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} catch (TagException e) {
    e.printStackTrace();
} catch (ReadOnlyFileException e) {
    e.printStackTrace();
} catch (InvalidAudioFrameException e) {
    e.printStackTrace();
}
if(f != null) {
    f.getTagOrCreateAndSetDefault();
    r_tag = f.getID3v2TagAsv24();
}
return r_tag;
链接地址: http://www.djcxy.com/p/96806.html

上一篇: 崩溃时,获取存储在SD中的图像数量

下一篇: Android使用jaudiotagger