0

I'm trying to change the album art of an audio file using jaudiotagger but it is not working. There are a lot of question regarding this topic but none of them fulfilled my requirements. Other fields such as artist,audio name, album etc are updated successfully but the album artwork remains the same.

The code I'm using to change the artwork:

  public void changeAlbumArt(Uri data) {


    Artwork cover = null;
    try {
        AudioFile f = null;
        try {
            f = AudioFileIO.read(new File(storageUtil.loadAudio().get(storageUtil.loadAudioIndex()).getData()));
        } 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();
        }
        Tag tag = null;
        if (f != null) {
            tag = f.getTag();
        }


        try {
            if(tag!=null) {
                cover = ArtworkFactory.createArtworkFromFile(new File(data.getPath()));
                tag.deleteArtworkField();
                tag.setField(cover);

            }
        } catch (FieldDataInvalidException e) {
            e.printStackTrace();
        }
        try {
            if (f != null) {
                f.commit();
            }
        } catch (CannotWriteException e) {
            e.printStackTrace();
        }


    } catch (Exception e) {
        e.printStackTrace();
    }

    File file = new File(storageUtil.loadAudio().get(storageUtil.loadAudioIndex()).getData());
    Uri uri = Uri.fromFile(file);
    Intent scanFileIntent = new Intent(
            Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
    sendBroadcast(scanFileIntent);


}
Rektirino
  • 582
  • 5
  • 24

1 Answers1

0

I had this working at one point but have reverted back to jid3 as jaudiotagger required some io files which were not available in android. In addition, for android Gingerbread+ to write to external storage requires the SAF framework. I have managed to modify the jid3 library so it works but only for mp3.

for mp3 files:

     public String setMp3AlbumArt(File SourceFile, byte[] bytes)
        throws Exception {
    String error = null;
    try {
        MP3File musicFile = (MP3File) AudioFileIO.read(SourceFile);
        AbstractID3v2Tag tag = musicFile.getID3v2Tag();
        if (tag != null) {
            Artwork artwork = null;
            try {
                artwork = ArtworkFactory.createArtworkFromFile(SourceFile);
            } catch (IOException e) {
                e.printStackTrace();
                error = e.getMessage();
            }
            if (artwork != null) {
                artwork.setBinaryData(bytes);
                tag.deleteArtworkField();
                tag.setField(artwork);
                musicFile.setTag(tag);
                musicFile.commit();
            } else {
                artwork.setBinaryData(bytes);
                tag.addField(artwork);
                tag.setField(artwork);
                musicFile.setTag(tag);
                musicFile.commit();
            }
        }

    } catch (CannotReadException | IOException | TagException
            | ReadOnlyFileException | InvalidAudioFrameException e) {
        error = e.getMessage();
    }
    return error;
}
Theo
  • 2,012
  • 1
  • 16
  • 29
  • Yeah, jaudiotagger has some missing io files but the 2.2.4 snapshot version works fine for kitkat and above(only tested kitkat and above). I'm having problem updating the album art right now. So you saying, it can't be done? – Rektirino Mar 25 '18 at 13:24
  • the code above worked for me so I suggest you give it a try – Theo Mar 25 '18 at 13:34
  • This piece of code is büt äs you know jaudiotagger also allows different formats. – Theo Mar 26 '18 at 06:19