1

How do I add a cover art (album art) to a downloaded mp3 programmatically?

I am letting my app download a mp3 file via the DownloadManager from the Android APIS as shown below:

private void startDownload(){
    Uri uri = Uri.parse(BASE_URL+DOWNLOAD+mp3+".mp3");

    filename = title+".mp3";

    Request request = new DownloadManager.Request(uri)
    .setDestinationInExternalPublicDir(MUSIC_PATH, filename);

    DOWNLOAD_MANAGER.enqueue(request);
}

Is it possible to add a coverart to the downloaded mp3 file, so that the mp3 player on the phone will show a image while playing the song?

Rany Ishak
  • 2,008
  • 2
  • 19
  • 19
sn0ep
  • 3,843
  • 8
  • 39
  • 63
  • 1
    Is the cover art embedded in these MP3 files? If so, then I think that the music / media player app will be able to sort that out for you on its own. If the cover is not embedded you will have to: 1) obtain a cover image file from somewhere, 2) [embed it into your MP3 file](http://www.richardfarrar.com/embedding-album-art-in-mp3-files/). – tiguchi Jul 16 '12 at 18:22

1 Answers1

9

Download this jar file MyID3_for_android and add it to your project's build path. here is a sample code of how you can change the cover

        File src = new File("filepath");
        MusicMetadataSet src_set = new MyID3().read(src);

        File img = (new File("imagePath"));


        Vector<ImageData> fileList = new Vector<ImageData>();
        ImageData data = new ImageData(readFile(img), "", "", 3);
        fileList.add(data);

        MusicMetadata metadata =  (MusicMetadata) src_set.getSimplified();
        metadata.setPictureList(fileList);

        File dst = new File("destinationPath");

        new MyID3().write(src, dst, src_set, metadata); 

You Will need also the Jakarta Regex jar

readFile is only a function to get byte[] from File

public static byte[] readFile (File file) throws IOException {
    // Open file
    RandomAccessFile f = new RandomAccessFile(file, "r");

    try {
        // Get and check length
        long longlength = f.length();
        int length = (int) longlength;
        if (length != longlength) throw new IOException("File size >= 2 GB");

        // Read file and return data
        byte[] data = new byte[length];
        f.readFully(data);
        return data;
    }
    finally {
        f.close();
    }
}

Got it from stackOverflow , dont know from who :)

Rany Ishak
  • 2,008
  • 2
  • 19
  • 19