gpt4 book ai didi

Android 用键盘发送图片

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:10:16 25 4
gpt4 key购买 nike

我正在尝试实现一个键盘应用程序,它应该能够将图像发送到当前 Activity (Whatsapp、消息应用程序等)。

有没有办法真正做到这一点?当然,它仅限于接受图像的应用程序,但我想知道最好的方法是什么。

尝试将 StringBuilder 与 ImageSpan 结合使用,但无法正常工作。我想知道是否有更好的方法。可以使用 Intent 吗?

最佳答案

最终通过向前台应用程序发送 Intent 来实现这一点,但这有局限性:消息传递应用程序通常需要选择对话,这会打断用户流程并增加不必要的步骤(除非它们公开了一种将 Intent 发送到具体聊天)。

这可以通过以下方式实现:

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sendIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
sendIntent.setPackage(getCurrentAppPackage(context, editorInfo));
return sendIntent;

其中 getCurrentAppPackage(...) 是一个返回前台 Activity 的方法,给定一个 Context 和一个 EditorInfo,你可以得到绑定(bind)到输入字段时来自您的 IME 实现。

public String getCurrentAppPackage(Context context, EditorInfo info) {
if(info != null && info.packageName != null) {
return info.packageName;
}
final PackageManager pm = context.getPackageManager();
//Get the Activity Manager Object
ActivityManager aManager =
(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
//Get the list of running Applications
List<ActivityManager.RunningAppProcessInfo> rapInfoList = aManager.getRunningAppProcesses();
//Iterate all running apps to get their details
for (ActivityManager.RunningAppProcessInfo rapInfo : rapInfoList) {
//error getting package name for this process so move on
if (rapInfo.pkgList.length == 0) {
Log.i("DISCARDED PACKAGE", rapInfo.processName);
continue;
}
try {
PackageInfo pkgInfo = pm.getPackageInfo(rapInfo.pkgList[0], PackageManager.GET_ACTIVITIES);
return pkgInfo.packageName;
} catch (PackageManager.NameNotFoundException e) {
// Keep iterating
}
}
return null;
}

更新:Commit Content API 添加到 API 级别 25(并且支持库使其从 API 13 开始工作)。更多信息在这里:https://developer.android.com/preview/image-keyboard.html在应用程序开始实现之前,上述方法可以用作后备。

关于Android 用键盘发送图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27379925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com