Android尝试使用资源未找到close()方法。

huangapple go评论81阅读模式
英文:

Android try with resources no method found close()

问题

我正在使用在 Android 应用程序中实现了 AutoCloseable 接口的 android MediaMetaDataRetriever。我有以下代码:

try (final MediaMetadataRetriever retriever = new MediaMetadataRetriever()) {
    retriever.setDataSource(videoUri.getPath());
    return retriever.getFrameAtTime(10, getFrameOption());
}

minSDK > 21

但是我遇到了以下崩溃:

在类 Landroid/media/MediaMetadataRetriever 中没有名为 close()V 的虚拟方法,也没有该类的超类('android.media.MediaMetadataRetriever' 的声明位于 /system/framework/framework.jar 中)

如果 MediaMetadataRetriever 实现了 AutoCloseable,为什么会发生这种情况呢?

英文:

I am using android MediaMetaDataRetriever which implements AutoCloseable in an android application. I have the below code

try (final MediaMetadataRetriever retriever = new MediaMetadataRetriever()) {
    retriever.setDataSource(videoUri.getPath());
    return retriever.getFrameAtTime(10, getFrameOption());
}

minSDK > 21

but I am getting the following crash

No virtual method close()V in class Landroid/media/MediaMetadataRetriever; or its super classes (declaration of ‘android.media.MediaMetadataRetriever’ appears in /system/framework/framework.jar

how can this happen if MediaMetadataRetriever implements AutoCloseable

答案1

得分: 4

以下是翻译好的内容:

我曾经遇到同样的问题,所以我在 Kotlin 中创建了自己的 MediaMetaDataRetriever 子类:

class MyMediaMetadataRetriever : MediaMetadataRetriever(), AutoCloseable {

   override fun close() {
      release()
   }

}

Java(未经测试):

public class MyMediaMetadataRetriever extends MediaMetadataRetriever implements AutoCloseable {

   public MyMediaMetadataRetriever() {
      super();
   }

   @Override
   public void close() {
      release();
   }

}
英文:

I had the same problem, so I created my own subclass of MediaMetaDataRetriever in Kotlin:

class MyMediaMetadataRetriever : MediaMetadataRetriever(), AutoCloseable {

   override fun close() {
      release()
   }

}

Java (untested):

public class MyMediaMetadataRetriever extends MediaMetadataRetriever implements AutoCloseable {

   public MyMediaMetadataRetriever() {
      super();
   }

   @Override
   public void close() {
      release();
   }

}

答案2

得分: 4

因为直到 API 29 之前,MediaMetadataRetriever 并没有实现 AutoCloseable 接口。所以在旧版平台上,close() 方法并不存在,这也是你遇到崩溃的原因。

在旧版平台上,你必须(手动)调用 release() 方法,这也是 close() 方法的实际委托对象。

不幸的是,这意味着除非你的 minSdk 版本设置为 29 或更高,否则你无法直接在 MediaMetadataRetriever 上使用 try-with-resources(或者 Kotlin 的 use)。

英文:

> how can this happen if MediaMetadataRetriever implements AutoCloseable.

Because MediaMetadataRetriever did not implement AutoCloseable until API 29. So on older platforms, the close() method does not exist, which is exactly what your crash is also saying.

On older platforms you have to (manually) call release() instead, which is what close() simply delegates to.

Unfortunately that means that you cannot use try-with-resources (or Kotlin's use) with MediaMetadataRetriever directly unless your minSdk is set to 29 or later.

huangapple
  • 本文由 发表于 2020年8月28日 18:22:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63631916.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定