【问题标题】:Get the size of an Android file resource?获取Android文件资源的大小?
【发布时间】:2011-08-28 08:10:30
【问题描述】:

我的资源中的 raw 文件夹中有一个视频文件。我想找到文件的大小。 我有这段代码:

Uri filePath = Uri.parse("android.resource://com.android.FileTransfer/" + R.raw.video);
                File videoFile = new File(filePath.getPath());
                Log.v("LOG", "FILE SIZE "+videoFile.length());

但它总是给我大小为 0。我做错了什么?

【问题讨论】:

    标签: android file resources size


    【解决方案1】:

    试试这个:

    AssetFileDescriptor sampleFD = getResources().openRawResourceFd(R.raw.video);
    long size = sampleFD.getLength()
    

    【讨论】:

    • 这看起来比其他答案安全得多。
    • +1 但默认情况下它不适用于许多文件类型,因为aapt 会压缩它们——有关解决方案,请参见此处:stackoverflow.com/questions/19743169/…
    【解决方案2】:

    试试这几行:

    InputStream ins = context.getResources().openRawResource (R.raw.video)
    int videoSize = ins.available();
    

    【讨论】:

    • 与我刚刚在@EboMike 的回答下发布的问题/评论相同。从来没有这样的问题??
    • openRawResource() 返回的不是普通的InputStream,而是AssetInputStream。该子类覆盖available() 并且能够提供可靠的信息。
    • @MarcoW。嗯,不是根据文档:developer.android.com/reference/android/content/res/…
    • @goldilocks:嗯,是的!如果您查看InputStream.available() 的文档,您会发现这就是解释的来源。 AssetInputStream 在其 JavaDoc 中使用完全相同的文本:developer.android.com/reference/java/io/… 但源显示 AssetInputStream.available() 覆盖超类的方法并从本机方法获取其结果:grepcode.com/file/repository.grepcode.com/java/ext/…
    • @MarcoW。我不怀疑这一点,也不怀疑它有一些实现(当前包括 android)可以工作。但是,如果您的标准是API 实际提交的内容,那么 API 不会对此作出承诺。如果它明天在某些设备上不起作用,那么相信基本编程原则说你不应该相信的东西将是你的错!此外,还有一个保险箱,按书本方法 - 请参阅 shem 的答案。这是规范的,IMO。
    【解决方案3】:

    您不能将File 用于资源。使用ResourcesAssetManager 获取资源的InputStream,然后对其调用available() 方法。

    像这样:

    InputStream is = context.getResources().openRawResource(R.raw.nameOfFile);
    int sizeOfInputStram = is.available(); // Get the size of the stream
    

    【讨论】:

    • 非常感谢 Ebo.. 它救了我的命!!
    • 从来没有遇到过这个问题?!这个方法应该返回an estimated number of bytes that can be read,不一定是真实的文件大小,文档明确不鼓励使用它...developer.android.com/reference/java/io/…
    • 好吧,文档指的是可能需要一段时间才能检索数据的通用流,例如网络流。在资源中,确切的大小始终是已知且可用的。
    【解决方案4】:

    answer@shem 略有不同

    AssetFileDescriptor afd = contentResolver.openAssetFileDescriptor(fileUri,"r");
    long fileSize = afd.getLength();
    afd.close();
    

    fileUri 的类型是 Android Uri

    【讨论】:

      【解决方案5】:

      可重用的 Kotlin 扩展

      您可以在上下文或活动中调用这些。它们是异常安全的

      fun Context.assetSize(resourceId: Int): Long =
          try {
              resources.openRawResourceFd(resourceId).length
          } catch (e: Resources.NotFoundException) {
              0
          }
      

      这个不如第一个好,但在某些情况下可能需要

      fun Context.assetSize(resourceUri: Uri): Long {
          try {
              val descriptor = contentResolver.openAssetFileDescriptor(resourceUri, "r")
              val size = descriptor?.length ?: return 0
              descriptor.close()
              return size
          } catch (e: Resources.NotFoundException) {
              return 0
          }
      }
      

      如果您想要一种简单的方法来获得不同的字节表示,您可以使用这些

      val Long.asKb get() = this.toFloat() / 1024
      val Long.asMb get() = asKb / 1024
      val Long.asGb get() = asMb / 1024 
      

      【讨论】:

        猜你喜欢
        • 2012-03-16
        • 2020-08-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-20
        • 1970-01-01
        • 1970-01-01
        • 2016-12-14
        相关资源
        最近更新 更多