【问题标题】:Add custom Item to Android share sheet?将自定义项目添加到 Android 共享表?
【发布时间】:2020-11-13 04:14:59
【问题描述】:

我有一个应用程序,我想让用户将布局保存到手机中的图像。我可以编写代码来创建位图并将其保存为图像格式。让我陷入困境的是,我无法在共享表中创建一个自定义选项,上面写着“保存到手机”。

val pm: PackageManager = context.getPackageManager()
val sendIntent = Intent(Intent.ACTION_SEND)

val img = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", File(fileName))

sendIntent.putExtra(Intent.EXTRA_STREAM, img)
sendIntent.type = "image/png"

val resInfo= pm.queryIntentActivities(sendIntent, 0)

val intentList: MutableList<LabeledIntent> = emptyList<LabeledIntent>().toMutableList()

for (res in resInfo){
    val intent = Intent()
    val packageName = res.activityInfo.packageName
    intent.component = ComponentName(packageName, res.activityInfo.name)
    intent.setPackage(packageName)
    intent.action = Intent.ACTION_SEND
    intentList.add(LabeledIntent(intent, packageName, res.loadLabel(pm), res.iconResource))
}

val xintent = Intent(context, GalleryFileSaver::class.java)
xintent.putExtra("savetogallery", imgUrl.toString())
val saveIntent = LabeledIntent(xintent, BuildConfig.APPLICATION_ID, "Save to phone", R.drawable.ic_download)

intentList.add(saveIntent)


val intentArray = intentList.toTypedArray()
sendIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray)
startActivity(Intent.createChooser(sendIntent, "Share Image"))

在我的清单上,

<activity android:name=".utils.GalleryFileSaver" android:screenOrientation="portrait"></activity>

GalleryFileSaver 是一个空白活动,它将图像渲染代码保存到oncreate。 我在intentArray 中添加了一个带标签的意图,但在共享表中只显示了默认意图,我做错了什么?

【问题讨论】:

    标签: java android kotlin android-intent android-activity


    【解决方案1】:

    您似乎需要将您的应用注册到receive implicit intents。对correct mime types 执行此操作后,它们应该会自动显示在意图选择器中。

    话虽如此,根据您的解释(如果我理解正确的话),您根本不需要使用共享表。您可以只添加一个按钮或菜单选项,例如“将当前布局保存到图像”,然后在您的应用中处理所有内容。

    【讨论】:

      【解决方案2】:

      编辑

      您正在添加所有可以将意图作为自定义目标处理的活动。这不是它的用途。 Intent.createChooser(...) 已经提供了正确的共享目标。无需再次全部添加。

      另外,自定义目标列表需要添加到Intent.createChooser(...)创建的Intentsource

      这应该可行:

      val sendIntent = Intent(Intent.ACTION_SEND)
      
      val img = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", File(fileName))
      
      sendIntent.putExtra(Intent.EXTRA_STREAM, img)
      sendIntent.type = "image/png"
      
      val xintent = Intent(context, GalleryFileSaver::class.java)
      xintent.putExtra("savetogallery", imgUrl.toString())
      val saveIntent = LabeledIntent(xintent, BuildConfig.APPLICATION_ID, "Save to phone", R.drawable.ic_download)
      
      startActivity(Intent.createChooser(sendIntent, "Share Image").apply{
          putExtra(Intent.EXTRA_INITIAL_INTENTS, arrayOf(saveIntent))
      })
      

      【讨论】:

      • 一样,没有变化
      • @Rivadiz 是的,我错过了一些东西,更新了我的答案
      猜你喜欢
      • 2011-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-05
      • 2023-04-02
      • 2014-03-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多