【发布时间】: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