Android sends image from keyboard

I am trying to implement a Keyboard application that should be able to send images to the current activity (Whatsapp, Messaging application, etc.).

Is there any way to achieve this? Of course, this will be limited to applications that accept images, but I wonder what the best approach is.

I tried using StringBuilder with ImageSpan, but could not get it to work. I was wondering if there is a better way. Perhaps through intentions?

+5
source share
1 answer

Finally, it succeeded by sending Intents to the front-end application, but this has limitations: messaging applications usually need to select a conversation that disrupts the user flow and adds an unnecessary step (unless they reveal a way to send intentions to a specific chat).

This can be achieved as follows:

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; 

Where getCurrentAppPackage(...) is a method that returns the front activity specified by a Context and EditorInfo , which you can get from your IME implementation when binding to an input field.

 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; } 

Update : The Content Content API was added at API level 25 (and the support library makes it work with API 13). More details here: https://developer.android.com/preview/image-keyboard.html Until the applications begin to implement it, the method described above can be used as a reserve.

+3
source

Source: https://habr.com/ru/post/1208663/


All Articles