使用jaudiotagger更改专辑封面

时间:2018-03-25 06:42:08

标签: android mediastore albumart jaudiotagger

我试图使用jaudiotagger更改音频文件的专辑封面,但它无法正常工作。关于这个主题有很多问题,但没有一个满足我的要求。艺术家,音频名称,专辑等其他字段已成功更新,但专辑图片保持不变。

我用来更改图稿的代码:

  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);


}

1 个答案:

答案 0 :(得分:0)

我有一点工作,但由于jaudiotagger需要一些在android中没有的io文件,所以已经恢复到jid3。 另外,对于android Gingerbread +写入外部存储需要SAF框架。我已经设法修改了jid3库,所以它可以工作但只适用于mp3。

用于mp3文件:

     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;
}