|
19 | 19 | package de.yaacc.upnp.server; |
20 | 20 |
|
21 | 21 | import android.annotation.SuppressLint; |
| 22 | +import android.content.ContentUris; |
22 | 23 | import android.content.Context; |
23 | 24 | import android.content.SharedPreferences; |
24 | 25 | import android.database.Cursor; |
25 | 26 | import android.graphics.Bitmap; |
26 | 27 | import android.graphics.drawable.BitmapDrawable; |
27 | 28 | import android.graphics.drawable.Drawable; |
28 | 29 | import android.net.Uri; |
| 30 | +import android.os.Build; |
29 | 31 | import android.provider.MediaStore; |
30 | 32 | import android.util.Log; |
| 33 | +import android.util.Size; |
31 | 34 |
|
32 | 35 | import androidx.core.content.res.ResourcesCompat; |
33 | 36 | import androidx.preference.PreferenceManager; |
|
54 | 57 |
|
55 | 58 | import java.io.ByteArrayOutputStream; |
56 | 59 | import java.io.File; |
| 60 | +import java.io.FileOutputStream; |
57 | 61 | import java.io.IOException; |
58 | 62 | import java.io.InputStream; |
59 | 63 | import java.io.RandomAccessFile; |
@@ -271,43 +275,64 @@ private ContentHolder lookupAlbumArt(String albumId, List<HttpRange> ranges) { |
271 | 275 | if (albumId == null) { |
272 | 276 | return result; |
273 | 277 | } |
274 | | - Log.d(getClass().getName(), "System media store lookup album: " |
275 | | - + albumId); |
276 | | - String[] projection = {MediaStore.Audio.Albums._ID, |
277 | | - // FIXME what is the right mime type? |
278 | | - // MediaStore.Audio.Albums.MIME_TYPE, |
279 | | - MediaStore.Audio.Albums.ALBUM_ART}; |
280 | | - String selection = MediaStore.Audio.Albums._ID + "=?"; |
281 | | - String[] selectionArgs = {albumId}; |
282 | | - try (Cursor cursor = getContext().getContentResolver().query( |
283 | | - MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, projection, |
284 | | - selection, selectionArgs, null)) { |
285 | | - |
286 | | - if (cursor != null) { |
287 | | - cursor.moveToFirst(); |
288 | | - while (!cursor.isAfterLast()) { |
289 | | - @SuppressLint("Range") String dataUri = cursor.getString(cursor |
290 | | - .getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART)); |
291 | | - |
292 | | - // String mimeTypeStr = null; |
293 | | - // FIXME mime type resolving cursor |
294 | | - // .getString(cursor |
295 | | - // .getColumnIndex(MediaStore.Files.FileColumns.MIME_TYPE)); |
296 | | - |
297 | | - MimeType mimeType = MimeType.valueOf("image/png"); |
298 | | - // if (mimeTypeStr != null) { |
299 | | - // mimeType = MimeType.valueOf(mimeTypeStr); |
300 | | - // } |
301 | | - if (dataUri != null) { |
302 | | - Log.d(getClass().getName(), "Content found: " + mimeType |
303 | | - + " Uri: " + dataUri); |
304 | | - result = new ContentHolder(mimeType, dataUri, ranges); |
| 278 | + if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) { |
| 279 | + Log.d(getClass().getName(), "System media store lookup album: " |
| 280 | + + albumId); |
| 281 | + String[] projection = {MediaStore.Audio.Albums._ID, |
| 282 | + // FIXME what is the right mime type? |
| 283 | + // MediaStore.Audio.Albums.MIME_TYPE, |
| 284 | + MediaStore.Audio.Albums.ALBUM_ART}; |
| 285 | + String selection = MediaStore.Audio.Albums._ID + "=?"; |
| 286 | + String[] selectionArgs = {albumId}; |
| 287 | + try (Cursor cursor = getContext().getContentResolver().query( |
| 288 | + MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, projection, |
| 289 | + selection, selectionArgs, null)) { |
| 290 | + |
| 291 | + if (cursor != null) { |
| 292 | + cursor.moveToFirst(); |
| 293 | + while (!cursor.isAfterLast()) { |
| 294 | + @SuppressLint("Range") String dataUri = cursor.getString(cursor |
| 295 | + .getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART)); |
| 296 | + |
| 297 | + // String mimeTypeStr = null; |
| 298 | + // FIXME mime type resolving cursor |
| 299 | + // .getString(cursor |
| 300 | + // .getColumnIndex(MediaStore.Files.FileColumns.MIME_TYPE)); |
| 301 | + |
| 302 | + MimeType mimeType = MimeType.valueOf("image/png"); |
| 303 | + // if (mimeTypeStr != null) { |
| 304 | + // mimeType = MimeType.valueOf(mimeTypeStr); |
| 305 | + // } |
| 306 | + if (dataUri != null) { |
| 307 | + Log.d(getClass().getName(), "Content found: " + mimeType |
| 308 | + + " Uri: " + dataUri); |
| 309 | + result = new ContentHolder(mimeType, dataUri, ranges); |
| 310 | + } |
| 311 | + cursor.moveToNext(); |
305 | 312 | } |
306 | | - cursor.moveToNext(); |
| 313 | + } else { |
| 314 | + Log.d(getClass().getName(), "System media store is empty."); |
307 | 315 | } |
308 | | - } else { |
309 | | - Log.d(getClass().getName(), "System media store is empty."); |
310 | 316 | } |
| 317 | + } else { |
| 318 | + Uri albumArtUri = ContentUris.withAppendedId(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, Long.parseLong(albumId)); |
| 319 | + MimeType mimeType = MimeType.valueOf("image/jpeg"); |
| 320 | + Log.d(getClass().getName(), "Content found: " + mimeType |
| 321 | + + " Uri: " + albumArtUri); |
| 322 | + try { |
| 323 | + Bitmap bitmap = context.getContentResolver().loadThumbnail(albumArtUri, new Size(1024, 1024), null); |
| 324 | + |
| 325 | + File art = new File(context.getCacheDir(), "albumart" + albumId + ".jpg"); |
| 326 | + art.createNewFile(); |
| 327 | + FileOutputStream fos = new FileOutputStream(art); |
| 328 | + bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos); |
| 329 | + fos.flush(); |
| 330 | + fos.close(); |
| 331 | + result = new ContentHolder(mimeType, art.getAbsolutePath(), ranges); |
| 332 | + } catch (IOException e) { |
| 333 | + Log.e(getClass().getName(), "Error loading album art", e); |
| 334 | + } |
| 335 | + |
311 | 336 | } |
312 | 337 | return result; |
313 | 338 | } |
|
0 commit comments