无法从内部目录读取 apk 文件

发布于 2025-01-10 22:59:35 字数 3623 浏览 1 评论 0 原文

我正在从 s3 下载一个 apk 并将其存储在名为 SAMPLE_APK 的目录中。当我尝试执行目录中存在的 apk 时,出现错误。

2022-03-01 15:44:20.773 21867-21867/com.test.digitaloceanspaces D/Main Activity -: File 
path  - /data/user/0/com.test.digitaloceanspaces/app_SAMPLE_APK/debug.apk
2022-03-01 15:44:20.774 21867-21867/com.test.digitaloceanspaces D/AndroidRuntime: 
Shutting down VM  

--------- beginning of crash
2022-03-01 15:44:20.779 21867-21867/com.test.digitaloceanspaces E/AndroidRuntime: FATAL 
EXCEPTION: main
    Process: com.test.digitaloceanspaces, PID: 21867
    java.lang.IllegalArgumentException: Failed to find configured root that contains 
/data/data/com.test.digitaloceanspaces/app_SAMPLE_APK/debug.apk
    at  
androidx.core.content.FileProvider$SimplePathStrategy
.getUriForFile(FileProvider.java:800)
    at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:442)

但是如果我从 filesDir 读取 apk,我就能够执行它。

下面两个不同的目录结构

无法读取回调方法的文件参数中存在的apk。

fun downloadLatestApk(callback: (File?, Exception?) -> Unit) {

//using a custom directory named sample apk
 val directoryName = "SAMPLE_APK"

    val directory = if (appContext.getDir(directoryName, Context.MODE_PRIVATE).exists()) 
    {
        Log.d("Local directory", "APK directory Already exists")
        appContext.getDir(directoryName, Context.MODE_PRIVATE)
    } else {
        Log.d("Local directory", "New Directory")
        File(appContext.filesDir, directoryName).apply {
            this.mkdir()
        }
    }

    val file = File(**directory**, "debug.apk")
    val listener = transferUtility.download(spacename, "debug.apk", file)

    listener.setTransferListener(object : TransferListener {

        override fun onStateChanged(id: Int, state: TransferState?) {
            if (state == TransferState.COMPLETED) {
                callback(file, null)
            }
        }
 }

能够执行回调方法的文件参数中存在的apk。在这里,我正在读取 filesDir 中存在的 apk。

fun downloadLatestAPk(callback: (File?, Exception?) -> Unit) {
//using files dir
val file = File(**appContext.filesDir**, "debug.apk")
    val listener = transferUtility.download(spacename, "debug.apk", file)

    listener.setTransferListener(object : TransferListener {

        override fun onStateChanged(id: Int, state: TransferState?) {
            if (state == TransferState.COMPLETED) {
                callback(file, null)
            }
        }
}

这只发生在 apk 上。我可以从两个目录中读取图像文件。无法理解这种行为。

文件夹结构 - 文件夹结构

文件提供程序具有以下路径。

<paths>
<external-path
    name="external"
    path="." />

<external-files-path
    name="external_files"
    path="." />

<cache-path
    name="cache"
    path="." />

<external-cache-path
    name="external_cache"
    path="." />

<files-path
    name="files"
    path="." />
</paths>

回调方法中的文件被发送到以下方法执行。

private fun handleInstallation(file: File) {

    Log.d("Main Activity - ", "File path  - ${file.path}")

    val contentUri =
        FileProvider.getUriForFile(
            this,
            "com.test.digitaloceanspaces" + ".provider", file
        )

    val intent = Intent()
    intent.action = Intent.ACTION_VIEW
    intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true)
    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_GRANT_READ_URI_PERMISSION
    intent.setDataAndType(contentUri, "application/vnd.android.package-archive")
    startActivity(intent)
}

打印日志语句后应用程序崩溃。 非常感谢任何帮助。

I'm downloading an apk from s3 and storing it in a directory named SAMPLE_APK. When I try to execute the apk present in the directory, I get an error.

2022-03-01 15:44:20.773 21867-21867/com.test.digitaloceanspaces D/Main Activity -: File 
path  - /data/user/0/com.test.digitaloceanspaces/app_SAMPLE_APK/debug.apk
2022-03-01 15:44:20.774 21867-21867/com.test.digitaloceanspaces D/AndroidRuntime: 
Shutting down VM  

--------- beginning of crash
2022-03-01 15:44:20.779 21867-21867/com.test.digitaloceanspaces E/AndroidRuntime: FATAL 
EXCEPTION: main
    Process: com.test.digitaloceanspaces, PID: 21867
    java.lang.IllegalArgumentException: Failed to find configured root that contains 
/data/data/com.test.digitaloceanspaces/app_SAMPLE_APK/debug.apk
    at  
androidx.core.content.FileProvider$SimplePathStrategy
.getUriForFile(FileProvider.java:800)
    at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:442)

But If I read the apk from filesDir, I'm able to execute it.

Below the two different directory structure

Unable to read the apk present in the file parameter of the callback method.

fun downloadLatestApk(callback: (File?, Exception?) -> Unit) {

//using a custom directory named sample apk
 val directoryName = "SAMPLE_APK"

    val directory = if (appContext.getDir(directoryName, Context.MODE_PRIVATE).exists()) 
    {
        Log.d("Local directory", "APK directory Already exists")
        appContext.getDir(directoryName, Context.MODE_PRIVATE)
    } else {
        Log.d("Local directory", "New Directory")
        File(appContext.filesDir, directoryName).apply {
            this.mkdir()
        }
    }

    val file = File(**directory**, "debug.apk")
    val listener = transferUtility.download(spacename, "debug.apk", file)

    listener.setTransferListener(object : TransferListener {

        override fun onStateChanged(id: Int, state: TransferState?) {
            if (state == TransferState.COMPLETED) {
                callback(file, null)
            }
        }
 }

Able to execute the apk present in the file parameter of the callback method. Here I'm reading the apk present in the filesDir.

fun downloadLatestAPk(callback: (File?, Exception?) -> Unit) {
//using files dir
val file = File(**appContext.filesDir**, "debug.apk")
    val listener = transferUtility.download(spacename, "debug.apk", file)

    listener.setTransferListener(object : TransferListener {

        override fun onStateChanged(id: Int, state: TransferState?) {
            if (state == TransferState.COMPLETED) {
                callback(file, null)
            }
        }
}

This happens only for apk. I'm able to read image files from either of the directories. Unable to fathom this behavior.

Folder structure - Folder Structure

The file provider has the following paths.

<paths>
<external-path
    name="external"
    path="." />

<external-files-path
    name="external_files"
    path="." />

<cache-path
    name="cache"
    path="." />

<external-cache-path
    name="external_cache"
    path="." />

<files-path
    name="files"
    path="." />
</paths>

File from the callback method is sent to the below method for execution.

private fun handleInstallation(file: File) {

    Log.d("Main Activity - ", "File path  - ${file.path}")

    val contentUri =
        FileProvider.getUriForFile(
            this,
            "com.test.digitaloceanspaces" + ".provider", file
        )

    val intent = Intent()
    intent.action = Intent.ACTION_VIEW
    intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true)
    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_GRANT_READ_URI_PERMISSION
    intent.setDataAndType(contentUri, "application/vnd.android.package-archive")
    startActivity(intent)
}

App crashes after printing the log statement.
Any help is deeply appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妖妓 2025-01-17 22:59:35

<块引用>

路径 - /data/user/0/com.test.digitaloceanspaces/app_SAMPLE_APK/debug.apk

这是 FileProvider 未覆盖的路径。

getFilesDir() 中的内容被条目 覆盖。

你可以做到:

/data/user/0/com.test.digitaloceanspaces/files/app_SAMPLE_APK/debug.apk

FileProvider未涵盖:java.lang.IllegalArgumentException:无法找到包含/data/data/com.test.digitaloceanspaces/app_SAMPLE_APK/debug.apk的配置根

<块引用>

无法从内部目录读取 apk 文件

否。无法将 FileProvider 用于不允许的路径。

path - /data/user/0/com.test.digitaloceanspaces/app_SAMPLE_APK/debug.apk

That is a path not covered by FileProvider.

From getFilesDir() is covered by entry <files-path.

You could make it:

/data/user/0/com.test.digitaloceanspaces/files/app_SAMPLE_APK/debug.apk

Not covered by FileProvider: java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.test.digitaloceanspaces/app_SAMPLE_APK/debug.apk

Unable to read an apk file from an internal directory

No. Unable to use FileProvider for not allowed path.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文