Android send Image with Keyboard
Asked Answered
D

1

6

I'm trying to implement a Keyboard app which should be capable of sending images to the current activity (Whatsapp, Messaging app, etc).

Is there a way to actually achieve this? Of course it would be limited to apps which accept images, but I wonder what's the best approach.

Tried using a StringBuilder with an ImageSpan but couldn't get it to work. I was wondering if there was a better way. Using Intents maybe?

Dutybound answered 9/12, 2014 at 13:20 Comment(2)
Have you found a solution yet?Dumb
"In previous versions of Android, soft keyboards (also known as input method editors or IMEs) could send only unicode emoji to apps. For rich content, apps had to either build app-specific APIs that couldn't be used in other apps or use workaround like sending images through Easy Share Action or the clipboard." developer.android.com/preview/image-keyboard.htmlConsultative
D
3

Finally achieved this by sending Intents to the foreground app, but this has limitations: messaging apps usually require to select the conversation, which breaks the user flow and adds an unnecessary step (unless they expose a way to send the intent 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 foreground Activity given a Context and an EditorInfo, which you can get from your IME implementation when binded 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: Commit Content API was added on API level 25 (and support library makes it work from API 13). More info here: https://developer.android.com/preview/image-keyboard.html Until apps start implementing it, the method above can be used as fallback.

Dutybound answered 5/12, 2016 at 14:19 Comment(3)
Hi Framundo, can you share your code displaying how you share to the foreground activity? Also, take note that regarding Commit Content, the app must be able to receive rich content and, as this is pretty new, not so many apps have it. So I think that both options must be implemented toghether.Dramaturge
I agree, edited the answer to include sample code and warn about fallbacking to the manual methodDutybound
@Dutybound pls send sample code for this bcz i'm working on same functionality and facing same issue.Tolerance

© 2022 - 2024 — McMap. All rights reserved.