read cover art for audio file (works for mp3) and displays it in the media player...
authorAndy Scherzinger <info@andy-scherzinger.de>
Fri, 14 Aug 2015 17:33:23 +0000 (19:33 +0200)
committerAndy Scherzinger <info@andy-scherzinger.de>
Fri, 14 Aug 2015 17:33:23 +0000 (19:33 +0200)
src/com/owncloud/android/ui/preview/PreviewMediaFragment.java

index 679308a..b197a85 100644 (file)
@@ -21,6 +21,9 @@ package com.owncloud.android.ui.preview;
 
 import android.accounts.Account;
 import android.app.Activity;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.media.MediaMetadataRetriever;
 import android.support.v7.app.AlertDialog;
 import android.content.ComponentName;
 import android.content.Context;
@@ -46,6 +49,7 @@ import android.view.View;
 import android.view.View.OnTouchListener;
 import android.view.ViewGroup;
 import android.widget.ImageView;
+import android.widget.LinearLayout;
 import android.widget.Toast;
 import android.widget.VideoView;
 
@@ -203,11 +207,35 @@ public class PreviewMediaFragment extends FileFragment implements
             } else {
                 mVideoPreview.setVisibility(View.GONE);
                 mImagePreview.setVisibility(View.VISIBLE);
+                extractAndSetCoverArt(file);
             }
         }
         
     }
-        
+
+    /**
+     * tries to read the cover art from the audio file and sets it as cover art.
+     *
+     * @param file audio file with potential cover art
+     */
+    private void extractAndSetCoverArt(OCFile file) {
+        if (file.isAudio()) {
+            try {
+                MediaMetadataRetriever mmr = new MediaMetadataRetriever();
+                mmr.setDataSource(file.getStoragePath());
+                byte[] data = mmr.getEmbeddedPicture();
+                if (data != null) {
+                    Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
+                    mImagePreview.setImageBitmap(bitmap); //associated cover art in bitmap
+                } else {
+                    mImagePreview.setImageResource(R.drawable.logo);
+                }
+            } catch (Throwable t) {
+                mImagePreview.setImageResource(R.drawable.logo);
+            }
+        }
+    }
+
 
     /**
      * {@inheritDoc}