fixed crash on older devices because of missing method

This commit is contained in:
Thibault Deckers 2020-05-14 18:18:15 +09:00
parent 6783a137cc
commit aa84512107
3 changed files with 20 additions and 4 deletions

View file

@ -153,7 +153,8 @@ public class MetadataHandler implements MethodChannel.MethodCallHandler {
// unnamed fallback directory // unnamed fallback directory
metadataMap.put("", dirMap); metadataMap.put("", dirMap);
try (MediaMetadataRetriever retriever = StorageUtils.openMetadataRetriever(context, Uri.parse(uri), path)) { MediaMetadataRetriever retriever = StorageUtils.openMetadataRetriever(context, Uri.parse(uri), path);
try {
for (Map.Entry<Integer, String> kv : Constants.MEDIA_METADATA_KEYS.entrySet()) { for (Map.Entry<Integer, String> kv : Constants.MEDIA_METADATA_KEYS.entrySet()) {
Integer key = kv.getKey(); Integer key = kv.getKey();
String value = retriever.extractMetadata(key); String value = retriever.extractMetadata(key);
@ -172,6 +173,9 @@ public class MetadataHandler implements MethodChannel.MethodCallHandler {
result.success(metadataMap); result.success(metadataMap);
} catch (Exception e) { } catch (Exception e) {
result.error("getAllVideoMetadataFallback-exception", "failed to get metadata for uri=" + uri + ", path=" + path, e.getMessage()); result.error("getAllVideoMetadataFallback-exception", "failed to get metadata for uri=" + uri + ", path=" + path, e.getMessage());
} finally {
// cannot rely on `MediaMetadataRetriever` being `AutoCloseable` on older APIs
retriever.release();
} }
} }
@ -240,7 +244,8 @@ public class MetadataHandler implements MethodChannel.MethodCallHandler {
} }
if (isVideo(mimeType)) { if (isVideo(mimeType)) {
try (MediaMetadataRetriever retriever = StorageUtils.openMetadataRetriever(context, Uri.parse(uri), path)) { MediaMetadataRetriever retriever = StorageUtils.openMetadataRetriever(context, Uri.parse(uri), path);
try {
String dateString = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DATE); String dateString = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DATE);
String rotationString = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION); String rotationString = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);
String locationString = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_LOCATION); String locationString = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_LOCATION);
@ -276,6 +281,9 @@ public class MetadataHandler implements MethodChannel.MethodCallHandler {
} }
} catch (Exception e) { } catch (Exception e) {
result.error("getCatalogMetadata-exception", "failed to get video metadata for uri=" + uri + ", path=" + path, e.getMessage()); result.error("getCatalogMetadata-exception", "failed to get video metadata for uri=" + uri + ", path=" + path, e.getMessage());
} finally {
// cannot rely on `MediaMetadataRetriever` being `AutoCloseable` on older APIs
retriever.release();
} }
} }
result.success(metadataMap); result.success(metadataMap);

View file

@ -24,7 +24,8 @@ class VideoThumbnailFetcher implements DataFetcher<InputStream> {
@Override @Override
public void loadData(@NonNull Priority priority, @NonNull DataCallback<? super InputStream> callback) { public void loadData(@NonNull Priority priority, @NonNull DataCallback<? super InputStream> callback) {
try (MediaMetadataRetriever retriever = StorageUtils.openMetadataRetriever(model.getContext(), model.getUri(), null)) { MediaMetadataRetriever retriever = StorageUtils.openMetadataRetriever(model.getContext(), model.getUri(), null);
try {
byte[] picture = retriever.getEmbeddedPicture(); byte[] picture = retriever.getEmbeddedPicture();
if (picture != null) { if (picture != null) {
callback.onDataReady(new ByteArrayInputStream(picture)); callback.onDataReady(new ByteArrayInputStream(picture));
@ -40,6 +41,9 @@ class VideoThumbnailFetcher implements DataFetcher<InputStream> {
} }
} catch (Exception ex) { } catch (Exception ex) {
callback.onLoadFailed(ex); callback.onLoadFailed(ex);
} finally {
// cannot rely on `MediaMetadataRetriever` being `AutoCloseable` on older APIs
retriever.release();
} }
} }

View file

@ -137,7 +137,8 @@ public class ImageEntry {
// expects entry with: uri/path, mimeType // expects entry with: uri/path, mimeType
// finds: width, height, orientation/rotation, date, title, duration // finds: width, height, orientation/rotation, date, title, duration
private void fillByMediaMetadataRetriever(Context context) { private void fillByMediaMetadataRetriever(Context context) {
try (MediaMetadataRetriever retriever = StorageUtils.openMetadataRetriever(context, uri, path)) { MediaMetadataRetriever retriever = StorageUtils.openMetadataRetriever(context, uri, path);
try {
String width = null, height = null, rotation = null, durationMillis = null; String width = null, height = null, rotation = null, durationMillis = null;
if (isImage()) { if (isImage()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
@ -177,6 +178,9 @@ public class ImageEntry {
} }
} catch (Exception e) { } catch (Exception e) {
// ignore // ignore
} finally {
// cannot rely on `MediaMetadataRetriever` being `AutoCloseable` on older APIs
retriever.release();
} }
} }