Adding Cover Art to downloaded mp3 on Android

sn0ep picture sn0ep · Jul 16, 2012 · Viewed 8.1k times · Source

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?

Answer

Rany Ishak picture Rany Ishak · Jul 19, 2012

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