我们如何在Android Q中访问扩展文件?

时间:2019-06-23 14:56:08

标签: android apk-expansion-files androidq

我正在写一个需要扩展文件的应用程序,我想确保它与Android Q兼容。看来documentation provided不能解决Android Q中的更改。在Android Q中,{{ 1}}将无法使用,那么我们如何访问扩展文件?

1 个答案:

答案 0 :(得分:3)

从问题中链接到的documentation,我们知道扩展文件的名称具有以下格式:

  

[main | patch]。<扩展版本>。<程序包名称> .obb

getObbDir()方法以以下形式返回扩展文件的特定位置:

  

<共享存储> / Android / obb / <程序包名称> /

所以,问题是我们如何访问此类文件?

为回答这个问题,我使用一个包含五个APK文件的目录,并使用JOBB创建了一个名为“ main.314159.com.example.opaquebinaryblob.obb”的OBB文件。我的意图是安装并读取此OBB文件,以在一个小型演示应用程序中显示APK文件名以及每个APK中的条目数(以Zip文件形式显示)。

演示应用程序还将尝试在外部存储目录下的各个目录中创建/读取测试文件。

以下操作是在运行最新版本“ Q”(Android 10.0(Google API))的Pixel XL模拟器上执行的。该应用程序具有以下特征:

  • targetSdkVersion 29
  • minSdkVersion 18
  • 无显式权限 清单中指定的

我向前看去,看到这个小应用返回了哪个目录getObbDir(),发现它是

/storage/emulated/0/Android/obb/com.example.opaquebinaryblob

所以我将OBB文件上传到了

/storage/emulated/0/Android/obb/com.example.opaquebinaryblob/main.314159.com.example.opaquebinaryblob.obb

使用Android Studio。这是文件整理的地方。

enter image description here

那么,我们可以挂载并读取此OBB文件吗?我们可以在外部文件路径内的其他目录中创建/读取文件吗?这是该应用程序根据API 29报告的内容:

enter image description here

唯一可访问的文件位于 /storage/emulated/0/Android/obb/com.example.opaquebinaryblob 中。层次结构中的其他文件无法创建或读取。 (有趣的是,可以确定这些文件的存在。)

对于前面的显示,该应用程序打开OBB文件并直接读取而无需安装。

当我们尝试挂载OBB文件并转储其内容时,将报告以下内容:

enter image description here

这是我们的期望。简而言之,Android Q似乎限制了对外部文件目录的访问,同时允许根据应用的程序包名称进行定向访问。

MainActivity.kt

class MainActivity : AppCompatActivity() {
    private lateinit var myObbFile: File
    private lateinit var mStorageManager: StorageManager

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        obbDumpText.movementMethod = ScrollingMovementMethod()

        val sb = StringBuilder()

        val extStorageDir = Environment.getExternalStorageDirectory()
        sb.appendln("getExternalStorageDirectory() reported at $extStorageDir").appendln()
        myObbFile = File(obbDir, BLOB_FILE_NAME)

        val obbDir = obbDir
        sb.appendln("obbDir reported at $obbDir").appendln()
        myObbFile = File(obbDir, BLOB_FILE_NAME)

        val directoryPathList = listOf(
            "$extStorageDir",
            "$extStorageDir/Pictures",
            "$extStorageDir/Android/obb/com.example.anotherpackage",
            "$extStorageDir/Android/obb/$packageName"
        )
        var e: Exception?
        for (directoryPath in directoryPathList) {
            val fileToCheck = File(directoryPath, TEST_FILE_NAME)
            e = checkFileReadability(fileToCheck)
            if (e == null) {
                sb.appendln("$fileToCheck is accessible.").appendln()
            } else {
                sb.appendln(e.message)
                try {
                    sb.appendln("Trying to create $fileToCheck")
                    fileToCheck.createNewFile()
                    sb.appendln("Created $fileToCheck")
                    e = checkFileReadability(fileToCheck)
                    if (e == null) {
                        sb.appendln("$fileToCheck is accessible").appendln()
                    } else {
                        sb.appendln("e").appendln()
                    }
                } catch (e: Exception) {
                    sb.appendln("Could not create $fileToCheck").appendln(e).appendln()
                }
            }
        }

        if (!myObbFile.exists()) {
            sb.appendln("OBB file doesn't exist: $myObbFile").appendln()
            obbDumpText.text = sb.toString()
            return
        }

        e = checkFileReadability(myObbFile)
        if (e != null) {
            // Need to request READ_EXTERNAL_STORAGE permission before reading OBB file
            sb.appendln("Need READ_EXTERNAL_STORAGE permission.").appendln()
            obbDumpText.text = sb.toString()
            return
        }

        sb.appendln("OBB is accessible at")
            .appendln(myObbFile).appendln()

        mStorageManager = getSystemService(Context.STORAGE_SERVICE) as StorageManager
        obbDumpText.text = sb.toString()
    }

    private fun dumpMountedObb(obbMountPath: String) {
        val obbFile = File(obbMountPath)

        val sb = StringBuilder().appendln("Dumping OBB...").appendln()
        sb.appendln("OBB file path is $myObbFile").appendln()
        sb.appendln("OBB mounted at $obbMountPath").appendln()
        val listFiles = obbFile.listFiles()
        if (listFiles == null || listFiles.isEmpty()) {
            Log.d(TAG, "No files in obb!")
            return
        }
        sb.appendln("Contents of OBB").appendln()
        for (listFile in listFiles) {
            val zipFile = ZipFile(listFile)
            sb.appendln("${listFile.name} has ${zipFile.entries().toList().size} entries.")
                .appendln()
        }
        obbDumpText.text = sb.toString()
    }

    private fun checkFileReadability(file: File): Exception? {
        if (!file.exists()) {
            return IOException("$file does not exist")
        }

        var inputStream: FileInputStream? = null
        try {
            inputStream = FileInputStream(file).also { input ->
                input.read()
            }
        } catch (e: IOException) {
            return e
        } finally {
            inputStream?.close()
        }
        return null
    }

    fun onClick(view: View) {
        mStorageManager.mountObb(
            myObbFile.absolutePath,
            null,
            object : OnObbStateChangeListener() {
                override fun onObbStateChange(path: String, state: Int) {
                    super.onObbStateChange(path, state)
                    val mountPath = mStorageManager.getMountedObbPath(myObbFile.absolutePath)
                    dumpMountedObb(mountPath)
                }
            }
        )
    }

    companion object {
        const val BLOB_FILE_NAME = "main.314159.com.example.opaquebinaryblob.obb"
        const val TEST_FILE_NAME = "TestFile.txt"
        const val TAG = "MainActivity"
    }
}

activity_main.xml

<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="16dp"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/obbDumpText"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scrollbars="vertical"
        android:text="Click the button to view content of the OBB."
        android:textColor="@android:color/black"
        app:layout_constraintBottom_toTopOf="@+id/dumpMountObb"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="spread_inside" />

    <Button
        android:id="@+id/dumpMountObb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="Dump\nMounted OBB"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/obbDumpText"
        app:layout_constraintVertical_bias="0.79" />
</androidx.constraintlayout.widget.ConstraintLayout>

对于here所述的后续活动:

  

从Android 4.4(API级别19)开始,应用无需外部存储权限即可读取OBB扩展文件。但是,Android 6.0(API级别23)及更高版本的某些实现仍需要权限,因此您需要在应用清单中声明READ_EXTERNAL_STORAGE权限,并在运行时请求权限...

这是否适用于Android Q?不清楚。该演示表明它不适用于仿真器。我希望这在不同设备之间是一致的。