从自定义键盘发送图像

时间:2017-05-06 09:33:29

标签: android bitmap custom-keyboard android-input-method

我有一个自定义键盘,它在画布上绘制作为输入,以编程方式转换为位图。现在我想将此位图图像发送到应用程序而不是普通文本。我如何提交图像而不是文本?

 sendImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            InputConnection ic = getCurrentInputConnection();
            if(mView.isCanvasDrawn()){
                Bitmap signature = getSignatureScreenshot(drawingView);
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                signature.compress(Bitmap.CompressFormat.PNG, 0, bos);
                ic.commitText(text,0); //this is for text but i want to send image
            }
        }
    });

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

将位图转换为base64并传递给您的服务器

答案 1 :(得分:0)

1)点击图片时,你必须提供图片网址 2)此权限是必需的(Intent.FLAG_ACTIVITY_NEW_TASK& FLAG_GRANT_READ_URI_PERMISSION)

 holder.ln_cover.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                      System.out.println("image path:"+mainSubList.get(position).getImage());

                        Picasso.with(getApplicationContext()).load(mainSubList.get(position).getImage()).into(new Target() {
                            @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {

                                System.out.println("bitmaploded called in andriod== "+ bitmap);
                                System.out.println("getLocalBitmapUri(bitmap)== "+ getLocalBitmapUri(bitmap));
                                Intent i = new Intent(Intent.ACTION_SEND);
                                i.setType("image/*");
                                i.putExtra(Intent.EXTRA_STREAM, getLocalBitmapUri(bitmap));
                                i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                                i.addFlags(Intent.FLAG_FROM_BACKGROUND);

                                context.getApplicationContext().startActivity(i);
                              //  subMenuAdapter.notifyDataSetChanged();

                                //startActivity(Intent.createChooser(i, "Share Image"));
                            }
                            @Override public void onBitmapFailed(Drawable errorDrawable) { }
                            @Override public void onPrepareLoad(Drawable placeHolderDrawable) { }
                        });



                    }
                });  

3)从Image url获取位图的方法

  public Uri getLocalBitmapUri(Bitmap bmp) {
    Uri bmpUri = null;
    try {
        File file =  new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
        FileOutputStream out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
        out.close();
        bmpUri = Uri.fromFile(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bmpUri;
}