我分享时在imageview上保存文字

时间:2016-01-01 09:14:17

标签: android draw

我在我的应用中捕获Image&我在图片上面写文字但是当我尝试分享它时,它没有显示它已经捕获了图像上的文字我只看到Image

这个应用程序就像Meme生成器应用程序..任何帮助将不胜感激。

btnShare.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                View content = findViewById(R.id.ivImage);
                content.setDrawingCacheEnabled(true);
                content.buildDrawingCache();
                Bitmap bitmap = ivImage.getDrawingCache();
                File root = Environment.getExternalStorageDirectory();
                File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg");
                try {
                    cachePath.createNewFile();
                    FileOutputStream ostream = new FileOutputStream(cachePath);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);
                    ostream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }


                Intent share = new Intent(Intent.ACTION_SEND);
                share.setType("image/*");
                share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(cachePath));
                startActivity(Intent.createChooser(share, "Share via"));

            }
        });

        ivImage = (ImageView) findViewById(R.id.ivImage);
        editText=(EditText)findViewById(R.id.editText);
        textView =(TextView)findViewById(R.id.textView);

3 个答案:

答案 0 :(得分:1)

而不是ImageView,您需要捕获布局图像。

替换此

View content = findViewById(R.id.ivImage);

View content = findViewById(yourLayoutID);

yourLayoutId =包含您想要分享的ImageView + TextView的布局。

答案 1 :(得分:0)

String text= "Write your text on image";
 Paint textPaint = new Paint();
    textPaint.setColor(Color.GREEN);
    textPaint.setTextSize(25);
    textPaint.setTextAlign(Align.RIGHT);

     Bitmap bitmap = ivImage.getDrawingCache();

    Canvas canvas = new Canvas(bitmap);
    canvas.drawText(text, 880, 30, textPaint);

答案 2 :(得分:0)

在您的代码中,您只调用图像以便文本不显示,您可以使用此代码从包含图像和文本的布局中获取所有图像,它将返回Bitmap,您可以共享它或者随心所欲地传递它,

private Bitmap getBitMapFromLayout(Context context ,Activity activity) {
        LayoutInflater mInflater = (LayoutInflater) activity.getSystemService(activity.LAYOUT_INFLATER_SERVICE);

        //Inflate the layout into a view and configure it the way you like
        RelativeLayout view = new RelativeLayout(activity);
        mInflater.inflate(R.layout.yourLayoutContainsImageAndTExt, view, true);

        //you can set Image or Text view here
        ImageView image = (ImageView) view.findViewById(R.id.image);
        TextView text = (TextView) view.findViewById(R.id.text);



        //Provide it with a layout params. It should necessarily be wrapping the
        //content as we not really going to have a parent for it.
        view.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT));

        //Pre-measure the view so that height and width don't remain null.
        view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

        //Assign a size and position to the view and all of its descendants
        view.layout(view.getMeasuredWidth(), view.getMeasuredHeight(), view.getMeasuredWidth(), view.getMeasuredHeight());

        //Create the bitmap
        Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(),
                view.getMeasuredHeight(),
                Bitmap.Config.ARGB_8888);
        //Create a canvas with the specified bitmap to draw into
        Canvas c = new Canvas(bitmap);

        //Render this view (and all of its children) to the given Canvas
        view.draw(c);
        return bitmap;
    }