必须在android中显示gif图像键盘
我使用以下代码在自定义键盘上单击按钮时显示gif图像、png图像。单击按钮时,图像将在悬挂区中发送,但无法在messenger中发送。按钮未处于启用状态 我要做的是设置按钮以在messenger中显示此自定义键盘时启用状态。请帮助我解决此问题 这是我的密码必须在android中显示gif图像键盘,android,keyboard,gif,ime,Android,Keyboard,Gif,Ime,我使用以下代码在自定义键盘上单击按钮时显示gif图像、png图像。单击按钮时,图像将在悬挂区中发送,但无法在messenger中发送。按钮未处于启用状态 我要做的是设置按钮以在messenger中显示此自定义键盘时启用状态。请帮助我解决此问题 这是我的密码 public class ImageKeyboard extends InputMethodService { private static final String TAG = "ImageKeyboard"; private stati
public class ImageKeyboard extends InputMethodService {
private static final String TAG = "ImageKeyboard";
private static final String AUTHORITY = "com.example.android.commitcontent.ime.inputcontent";
private static final String MIME_TYPE_GIF = "image/gif";
private static final String MIME_TYPE_PNG = "image/png";
private static final String MIME_TYPE_WEBP = "image/webp";
private File mPngFile;
private File mGifFile;
private File mWebpFile;
private Button mGifButton;
private Button mPngButton;
private Button mWebpButton;
private boolean isCommitContentSupported(
@Nullable EditorInfo editorInfo, @NonNull String mimeType) {
if (editorInfo == null) {
return false;
}
final InputConnection ic = getCurrentInputConnection();
if (ic == null) {
return false;
}
if (!validatePackageName(editorInfo)) {
return false;
}
final String[] supportedMimeTypes = EditorInfoCompat.getContentMimeTypes(editorInfo);
for (String supportedMimeType : supportedMimeTypes) {
if (ClipDescription.compareMimeTypes(mimeType, supportedMimeType)) {
return true;
}
}
return false;
}
private void doCommitContent(@NonNull String description, @NonNull String mimeType,
@NonNull File file) {
final EditorInfo editorInfo = getCurrentInputEditorInfo();
// Validate packageName again just in case.
if (!validatePackageName(editorInfo)) {
return;
}
final Uri contentUri = FileProvider.getUriForFile(this, AUTHORITY, file);
// As you as an IME author are most likely to have to implement your own content provider
// to support CommitContent API, it is important to have a clear spec about what
// applications are going to be allowed to access the content that your are going to share.
final int flag;
if (Build.VERSION.SDK_INT >= 25) {
// On API 25 and later devices, as an analogy of Intent.FLAG_GRANT_READ_URI_PERMISSION,
// you can specify InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION to give
// a temporary read access to the recipient application without exporting your content
// provider.
flag = InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION;
} else {
// On API 24 and prior devices, we cannot rely on
// InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION. You as an IME author
// need to decide what access control is needed (or not needed) for content URIs that
// you are going to expose. This sample uses Context.grantUriPermission(), but you can
// implement your own mechanism that satisfies your own requirements.
flag = 0;
try {
// TODO: Use revokeUriPermission to revoke as needed.
grantUriPermission(
editorInfo.packageName, contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
} catch (Exception e){
Log.e(TAG, "grantUriPermission failed packageName=" + editorInfo.packageName
+ " contentUri=" + contentUri, e);
}
}
final InputContentInfoCompat inputContentInfoCompat = new InputContentInfoCompat(
contentUri,
new ClipDescription(description, new String[]{mimeType}),
null /* linkUrl */);
InputConnectionCompat.commitContent(
getCurrentInputConnection(), getCurrentInputEditorInfo(), inputContentInfoCompat,
flag, null);
}
private boolean validatePackageName(@Nullable EditorInfo editorInfo) {
if (editorInfo == null) {
return false;
}
final String packageName = editorInfo.packageName;
if (packageName == null) {
return false;
}
// In Android L MR-1 and prior devices, EditorInfo.packageName is not a reliable identifier
// of the target application because:
// 1. the system does not verify it [1]
// 2. InputMethodManager.startInputInner() had filled EditorInfo.packageName with
// view.getContext().getPackageName() [2]
// [1]: https://android.googlesource.com/platform/frameworks/base/+/a0f3ad1b5aabe04d9eb1df8bad34124b826ab641
// [2]: https://android.googlesource.com/platform/frameworks/base/+/02df328f0cd12f2af87ca96ecf5819c8a3470dc8
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return true;
}
final InputBinding inputBinding = getCurrentInputBinding();
if (inputBinding == null) {
// Due to b.android.com/225029, it is possible that getCurrentInputBinding() returns
// null even after onStartInputView() is called.
// TODO: Come up with a way to work around this bug....
Log.e(TAG, "inputBinding should not be null here. "
+ "You are likely to be hitting b.android.com/225029");
return false;
}
final int packageUid = inputBinding.getUid();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
final AppOpsManager appOpsManager =
(AppOpsManager) getSystemService(Context.APP_OPS_SERVICE);
try {
appOpsManager.checkPackage(packageUid, packageName);
} catch (Exception e) {
return false;
}
return true;
}
final PackageManager packageManager = getPackageManager();
final String possiblePackageNames[] = packageManager.getPackagesForUid(packageUid);
for (final String possiblePackageName : possiblePackageNames) {
if (packageName.equals(possiblePackageName)) {
return true;
}
}
return false;
}
@Override
public void onCreate() {
super.onCreate();
// TODO: Avoid file I/O in the main thread.
final File imagesDir = new File(getFilesDir(), "images");
imagesDir.mkdirs();
mGifFile = getFileForResource(this, R.raw.animated_gif, imagesDir, "image.gif");
mPngFile = getFileForResource(this, R.raw.dessert_android, imagesDir, "image.png");
mWebpFile = getFileForResource(this, R.raw.animated_webp, imagesDir, "image.webp");
}
@Override
public View onCreateInputView() {
mGifButton = new Button(this);
mGifButton.setText("Insert GIF");
mGifButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ImageKeyboard.this.doCommitContent("A waving flag", MIME_TYPE_GIF, mGifFile);
}
});
mPngButton = new Button(this);
mPngButton.setText("Insert PNG");
mPngButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ImageKeyboard.this.doCommitContent("A droid logo", MIME_TYPE_PNG, mPngFile);
}
});
mWebpButton = new Button(this);
mWebpButton.setText("Insert WebP");
mWebpButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ImageKeyboard.this.doCommitContent(
"Android N recovery animation", MIME_TYPE_WEBP, mWebpFile);
}
});
final LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(mGifButton);
layout.addView(mPngButton);
layout.addView(mWebpButton);
return layout;
}
@Override
public boolean onEvaluateFullscreenMode() {
// In full-screen mode the inserted content is likely to be hidden by the IME. Hence in this
// sample we simply disable full-screen mode.
return false;
}
@Override
public void onStartInputView(EditorInfo info, boolean restarting) {
mGifButton.setEnabled(mGifFile != null && isCommitContentSupported(info, MIME_TYPE_GIF));
mPngButton.setEnabled(mPngFile != null && isCommitContentSupported(info, MIME_TYPE_PNG));
mWebpButton.setEnabled(mWebpFile != null && isCommitContentSupported(info, MIME_TYPE_WEBP));
}
private static File getFileForResource(
@NonNull Context context, @RawRes int res, @NonNull File outputDir,
@NonNull String filename) {
final File outputFile = new File(outputDir, filename);
final byte[] buffer = new byte[4096];
InputStream resourceReader = null;
try {
try {
resourceReader = context.getResources().openRawResource(res);
OutputStream dataWriter = null;
try {
dataWriter = new FileOutputStream(outputFile);
while (true) {
final int numRead = resourceReader.read(buffer);
if (numRead <= 0) {
break;
}
dataWriter.write(buffer, 0, numRead);
}
return outputFile;
} finally {
if (dataWriter != null) {
dataWriter.flush();
dataWriter.close();
}
}
} finally {
if (resourceReader != null) {
resourceReader.close();
}
}
} catch (IOException e) {
return null;
}
}
}
公共类ImageKeyboard扩展InputMethodService{
私有静态最终字符串标记=“ImageKeyboard”;
私有静态最终字符串AUTHORITY=“com.example.android.commitcontent.ime.inputcontent”;
私有静态最终字符串MIME\u TYPE\u GIF=“image/GIF”;
私有静态最终字符串MIME\u TYPE\u PNG=“image/PNG”;
私有静态最终字符串MIME\u TYPE\u WEBP=“image/WEBP”;
私有文件mPngFile;
私有文件管理文件;
私有文件mWebpFile;
专用按钮mGifButton;
私人按钮mPngButton;
私人按钮mWebpButton;
私有布尔值isCommitContentSupported(
@可为null的EditorInfo EditorInfo,@NonNull字符串mimeType){
如果(editorInfo==null){
返回false;
}
最终输入连接ic=getCurrentInputConnection();
如果(ic==null){
返回false;
}
如果(!validatePackageName(editorInfo)){
返回false;
}
最后一个字符串[]SupportedTimeTypes=EditorInfoCompat.getContentMimeTypes(editorInfo);
对于(字符串SupportedMiType:SupportedMiType){
if(ClipDescription.CompareMetypes(mimeType,SupportedMMetype)){
返回true;
}
}
返回false;
}
私有void doCommitContent(@NonNull String description,@NonNull String mimeType,
@非空文件(非空文件){
final EditorInfo EditorInfo=GetCurrentInputitorInfo();
//请再次验证我,以防万一。
如果(!validatePackageName(editorInfo)){
返回;
}
最终Uri contentUri=FileProvider.getUriForFile(这个,权限,文件);
//作为IME作者,您很可能必须实现自己的内容提供商
//为了支持CommitContentAPI,有一个关于什么的清晰规范是很重要的
//应用程序将被允许访问您将要共享的内容。
最终int标志;
如果(Build.VERSION.SDK_INT>=25){
//在API 25及更高版本的设备上,类似于Intent.FLAG\u GRANT\u READ\u URI\u权限,
//您可以指定InputConnectionCompat.INPUT\u内容\u授予\u读取\u URI\u权限以授予
//对收件人应用程序的临时读取访问,而不导出您的内容
//提供者。
flag=InputConnectionCompat.INPUT\u CONTENT\u GRANT\u READ\u URI\u PERMISSION;
}否则{
//在API 24和之前的设备上,我们不能依赖
//InputConnectionCompat.INPUT\u内容\u授予\u读取\u URI\u权限。您作为IME作者
//需要决定哪些内容URI需要(或不需要)访问控制
//您将公开。此示例使用Context.grantUriPermission(),但您可以
//实现您自己的机制,以满足您自己的需求。
flag=0;
试一试{
//TODO:根据需要使用revokeUriPermission进行撤销。
格兰特雷伯酒店(
editorInfo.packageName、contentUri、Intent.FLAG(授予读取URI权限);
}捕获(例外e){
Log.e(标记“grantUriPermission失败的packageName=“+editorInfo.packageName
+“contentUri=”+contentUri,e);
}
}
最终InputContentInfoCompat InputContentInfoCompat=新InputContentInfoCompat(
孔图里,
新ClipDescription(描述,新字符串[]{mimeType}),
null/*linkUrl*/);
输入连接compat.committecontent(
getCurrentInputConnection(),GetCurrentInputitorInfo(),inputContentInfoCompat,
标志,空);
}
私有布尔validatePackageName(@Nullable EditorInfo EditorInfo){
如果(editorInfo==null){
返回false;
}
最后一个字符串packageName=editorInfo.packageName;
if(packageName==null){
返回false;
}
//在Android L MR-1和以前的设备中,EditorInfo.packageName不是可靠的标识符
//目标应用程序的错误,因为:
//1.系统未对其进行验证[1]
//2.InputMethodManager.startInputInner()已将
//view.getContext().getPackageName()[2]
// [1]: https://android.googlesource.com/platform/frameworks/base/+/a0f3ad1b5aabe04d9eb1df8bad34124b826ab641
// [2]: https://android.googlesource.com/platform/frameworks/base/+/02df328f0cd12f2af87ca96ecf5819c8a3470dc8
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.M){
返回true;
}
final InputBinding InputBinding=getCurrentInputBinding();
if(inputBinding==null){
//由于b.android.com/225029的原因,getCurrentInputBinding()可能返回
//即使在调用onStartInputView()之后也为null。
//TODO:想出一种解决此错误的方法。。。。
Log.e(标记,“inputBinding在此不应为null。”
+“您可能正在访问b.android.com/225029”);
返回false;
}
final int-packageUid=inputBinding.getUid();
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.KITKAT){
最终任命经理任命经理=
(apposmanager)getSystemService(Context.APP_OPS_SERVICE);
试一试{
AppManager.checkPackage(packageUid,packageName);
}捕获(例外e){
返回false;
}
返回true;
}
最终PackageManager PackageManager=getPackageManager();
最后一个字符串可能是packagenames[]=packageManager.getPackagesForUid(packageUid);
for(最终字符串possiblePackageName:possiblePackageNames){
if(packageName.equals(可能的packageName)){