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
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()) {
Integer key = kv.getKey();
String value = retriever.extractMetadata(key);
@ -172,6 +173,9 @@ public class MetadataHandler implements MethodChannel.MethodCallHandler {
result.success(metadataMap);
} catch (Exception e) {
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)) {
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 rotationString = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);
String locationString = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_LOCATION);
@ -276,6 +281,9 @@ public class MetadataHandler implements MethodChannel.MethodCallHandler {
}
} catch (Exception e) {
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);

View file

@ -24,7 +24,8 @@ class VideoThumbnailFetcher implements DataFetcher<InputStream> {
@Override
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();
if (picture != null) {
callback.onDataReady(new ByteArrayInputStream(picture));
@ -40,6 +41,9 @@ class VideoThumbnailFetcher implements DataFetcher<InputStream> {
}
} catch (Exception 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
// finds: width, height, orientation/rotation, date, title, duration
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;
if (isImage()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
@ -177,6 +178,9 @@ public class ImageEntry {
}
} catch (Exception e) {
// ignore
} finally {
// cannot rely on `MediaMetadataRetriever` being `AutoCloseable` on older APIs
retriever.release();
}
}