首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用意图共享此应用程序base.apk?

如何使用意图共享此应用程序base.apk?
EN

Stack Overflow用户
提问于 2022-06-26 06:02:57
回答 2查看 123关注 0票数 0

我想使用共享按钮来共享我的应用程序。单击按钮后,它应该从包管理器中获取base.apk,然后使用意图共享它。

以下是我到目前为止所拥有的:

  1. 所有的用户界面都准备好了,可以工作了。
  2. 我有下面的代码来获取和分享这个应用程序 尝试{ val = packageManager val ai = pm.getApplicationInfo(packageName,0) val srcFile = File(ai.publicSourceDir) val共享= Intent.ACTION_SEND share.type =“application/vnd.android.Package”share.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(srcFile)) startActivity(共享,} catch (e: Exception) {UtilityMethods(This).toast(“未能共享应用程序”,"e") e.printStackTrace() }

但是我发现这个程序有个错误。

代码语言:javascript
复制
android.os.FileUriExposedException: file:///data/app/~~BC-clKZDViP_O7n44ooPbQ%3D%3D/MyAppPublicSourceDirectory/base.apk exposed beyond app through ClipData.Item.getUri()

在这件事上我能得到什么帮助吗?我尝试了很多解决方案,但它们对我不起作用。

编辑::更新的代码,复制base.apk到下载文件夹并重命名它。然后尝试共享它(这是错误调用的地方)。

代码语言:javascript
复制
try {
                // get the base apk of the app
                val pm = packageManager
                val ai = pm.getApplicationInfo(packageName, 0)
                val srcFile = File(ai.publicSourceDir)

                // save the file in Downloads folder
                val dstFile = File(
                    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
                    "LogsCalculator.apk"
                )
                dstFile.createNewFile()
                val input = FileInputStream(srcFile)
                val output = FileOutputStream(dstFile)
                val buffer = ByteArray(1024)
                var length: Int = input.read(buffer)
                while (length > 0) {
                    output.write(buffer, 0, length)
                    length = input.read(buffer)
                }
                output.flush()
                output.close()
                input.close()

                // share the apk file now
                val intent = Intent(Intent.ACTION_SEND)
                intent.type = "application/vnd.android.package-archive"
                intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(dstFile))
                startActivity(Intent.createChooser(intent, getString(R.string.sharing)))

            } catch (e: Exception) {
                UtilityMethods(this).toast("Failed To Share The App", "e")
                e.printStackTrace()
            }

它还是不起作用。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-06-28 00:44:14

我找到解决办法了。

代码语言:javascript
复制
try {
                // get the base.apk
                val baseApkLocation =
                    applicationContext.packageManager.getApplicationInfo(
                        applicationContext.packageName,
                        PackageManager.GET_META_DATA
                    ).sourceDir
                // get the file
                val baseApk = File(baseApkLocation)

                // the path
                val path = Environment.getExternalStorageDirectory().toString() + "/Download/"
                // make the directory
                val dir = File(path)
                // if the directory doesn't exist, make it
                if (!dir.exists()) {
                    dir.mkdirs()
                }

                // Copy the .apk file to downloads directory
                val destination = File(
                    path + "MyAppName.apk"
                )
                if (destination.exists()) {
                    destination.delete()
                }
                destination.createNewFile()
                val input = FileInputStream(baseApk)
                val output = FileOutputStream(destination)
                val buffer = ByteArray(1024)
                var length: Int = input.read(buffer)
                while (length > 0) {
                    output.write(buffer, 0, length)
                    length = input.read(buffer)
                }
                output.flush()
                output.close()
                input.close()

                // get content uri for the file
                val uri = FileProvider.getUriForFile(
                    this,
                    BuildConfig.APPLICATION_ID + ".provider",
                    destination
                )

                // share the file
                val intent = Intent(Intent.ACTION_SEND)
                intent.type = "application/vnd.android.package-archive"
                intent.putExtra(Intent.EXTRA_STREAM, uri)
                startActivity(Intent.createChooser(intent, getString(R.string.share_app)))

            } catch (e: Exception) {
                Lib(this).toast("Failed To Share The App", "e")
                e.printStackTrace()
            }

下面是它的工作原理

  1. 从其源dir获取应用程序的base.apk文件。
  2. 将文件复制到新位置,并给它一个有意义的名称。
  3. 获取新文件的内容URI。这就是丢失的东西。 //获取文件val = FileProvider.getUriForFile( this,BuildConfig.APPLICATION_ID + ".provider",目的)的内容uri
  4. 分享这份文件。

不管怎样,谢谢你的帮助。

票数 0
EN

Stack Overflow用户

发布于 2022-06-27 13:39:53

在以后的API版本中,即使是备份工具也无法获得APK,因此这种方法实际上是毫无意义的。相反,使用火源动态链接,以便允许用户共享您的应用程序。通过这种方式,他们可以从Google安装,而不是安装一些未知来源的APK,这可能无法很好地更新。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72759240

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档