5

我想實現它應該是能夠將圖片發送到當前活動(的WhatsApp,短信應用等)的鍵盤應用程序。的Android發送圖像的鍵盤

有沒有辦法真正實現這一目標?當然,它僅限於接受圖像的應用程序,但我不知道最佳方法是什麼。

使用帶ImageSpan StringBuilder的嘗試,但不能讓它開始工作。 我想知道是否有更好的方法。使用intents也許?

+0

您是否找到了解決方案? – 2016-04-21 10:13:15

+0

「在以前的Android版本中,軟鍵盤(也稱爲輸入法編輯器或IME)只能嚮應用程序發送unicode表情符號。對於豐富的內容,應用程序必須構建特定於應用程序的API,這些API無法在其他應用程序中使用或使用通過Easy Share Action或剪貼板發送圖像的解決方法。「 https://developer.android.com/preview/image-keyboard.html – hrules6872 2016-11-22 14:30:17

回答

3

最後通過發送意圖到前臺應用程序實現了這一點,但是這也有侷限性:消息應用程序通常需要選擇對話,打破了用戶流量,並增加了不必要的步驟(除非他們暴露了一種意圖發送到特定聊天)。

這是可以實現如下:

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(...)是返回給定ContextEditorInfo前景活動,您可以從您的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; 
} 

更新: 提交內容API是在API級別25(和支持庫,可以從API 13工作)加入。更多信息:https://developer.android.com/preview/image-keyboard.html 在應用程序開始實施它之前,上面的方法可以用作備用。

+0

嗨Framundo,你能分享你的代碼顯示你如何分享到前臺活動? 另外,請注意,關於提交內容,應用程序必須能夠接收豐富的內容,並且,由於這是相當新穎的,所以沒有那麼多應用程序擁有它。所以我認爲這兩個選擇都必須實施。 – 2017-01-12 12:15:16

+0

我同意,編輯答案包括示例代碼並警告關於後備到手動方法 – framundo 2017-01-13 13:24:25