title: Is it possible to retrieve album art from remote mp3 file in Android?
body: <p>I'm currently writing a UPnP remote control app which is used to connect a remote MediaServer to a remote MediaRenderer.  Since the actual MP3 files aren't sent to the Android device, I'd like to be able to get the album art of the currently playing file without having to download the entire MP3 file to my phone.</p>
  
  <p>I've read that <code>MediaMetadataRetriever</code> is useful for this kind of thing, but I haven't been able to get it to work.  Each way I try it, I keep getting an <code>IllegalArgumentException</code> by the call to <code>MediaMetadataRetriever#setDataSource</code>, which indicates that my file handle or URI is invalid.</p>
  
  <pre><code>MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
  </code></pre>
  
  <p>The following works since it's a direct file path on the device itself:</p>
  
  <pre><code>metaRetriever.setDataSource("/sdcard/Music/Daft_Punk/Homework/01 - Daftendirekt.mp3");
  </code></pre>
  
  <p>However, any of the following fail with the same error:</p>
  
  <pre><code>metaRetriever.setDataSource(appCtx, Uri.parse("http://192.168.1.144:49153/content/media/object_id/94785/res_id/1/rct/aa"));
  metaRetriever.setDataSource(appCtx, Uri.parse("http://192.168.1.144:49153/content/media/object_id/94785/res_id/0/ext/file.mp3"));
  metaRetriever.setDataSource("http://192.168.1.144:49153/content/media/object_id/94785/res_id/0/ext/file.mp3");
  </code></pre>
  
  <p>The first one is the albumArtURI pulled from the UPnP metadata (no *.mp3 extension, but the file will download if pasted into a web browser).</p>
  
  <p>The second and third attempts are using the "res" value from the UPnP metadata, which points to the actual file on the server.</p>
  
  <p>I'm hoping I'm just parsing the URI incorrectly, but I'm out of ideas.</p>
  
  <p>Any suggestions?  Also, is there a better way to do this entirely when pulling from a UPnP server?  FWIW, I'm using the Cling UPnP library.</p>
  
  <p>== SOLUTION ==</p>
  
  <p>I started looking into william-seemann's answer and it led me to this: <a href="https://code.google.com/p/android/issues/detail?id=35794" rel="nofollow">MediaMetadataRetriever.setDataSource(String path) no longer accepts URLs</a></p>
  
  <p>Comment #2 on this post mentions using a different version of <code>setDataSource()</code> that still accepts remote URLs.</p>
  
  <p>Here's what I ended up doing and it's working great:</p>
  
  <pre><code>private Bitmap downloadBitmap(final String url) {
       final MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
       metaRetriever.setDataSource(url, new HashMap&lt;String, String&gt;());
       try {
          final byte[] art = metaRetriever.getEmbeddedPicture();
          return BitmapFactory.decodeByteArray(art, 0, art.length);
       } catch (Exception e) {
          Logger.e(LOGTAG, "Couldn't create album art: " + e.getMessage());
          return BitmapFactory.decodeResource(getResources(), R.drawable.album_art_missing);
       }
    }
  </code></pre>