我正在尝试实现一个键盘应用程序,它应该能够将图像发送到当前活动(Whatsapp、短信应用等)。
有没有方法可以实现这一点?当然,它将被限制在接受图像的应用程序中,但我想知道最佳方法是什么。
尝试使用带有ImageSpan的StringBuilder,但无法使其工作。我想知道是否有更好的方法。也许可以使用意图(Intents)?
我正在尝试实现一个键盘应用程序,它应该能够将图像发送到当前活动(Whatsapp、短信应用等)。
有没有方法可以实现这一点?当然,它将被限制在接受图像的应用程序中,但我想知道最佳方法是什么。
尝试使用带有ImageSpan的StringBuilder,但无法使其工作。我想知道是否有更好的方法。也许可以使用意图(Intents)?
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;
在绑定到输入字段时,您可以从您的 IME 实现中获取一个 Context
和一个 EditorInfo
,其中 getCurrentAppPackage(...)
是一个方法,它返回前台 Activity。
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;
}
更新:在API级别25中添加了Commit Content API(支持库可使其在API 13上工作)。更多信息请参见:https://developer.android.com/preview/image-keyboard.html。 在应用程序开始实施之前,可以使用以上方法作为备选方案。