1

I want to edit an image by adding text on top of an existing image. I tried this, but the drawable in the ImageView disappears:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    Button btnAdd=(Button) findViewById(R.id.btnAdd);
    btnAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EditText firstNum=(EditText)findViewById(R.id.fisrtNum);
            EditText secondNum=(EditText)findViewById(R.id.secondNum);
            TextView sumTV=(TextView)findViewById(R.id.sumTV);
            ImageView picIV=(ImageView) findViewById(R.id.picIV);

            Bitmap bitmap = Bitmap.createBitmap(300, 200, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

            paint.setTextSize(42);
            paint.setTextAlign(Paint.Align.CENTER);
            canvas.drawText("hello world",150,30,paint);
            paint.setColor(Color.BLUE);
            canvas.drawCircle(50, 50, 10, paint);
            picIV.setImageBitmap(bitmap);

            picIV.setImageDrawable(new BitmapDrawable(getResources(), bitmap));
            sumTV.setText(result+"");

        }
    }
}

Thank you in advance for helping find a solution. My goal is to be able to edit an image and to send it to other apps.

Chewee133
  • 55
  • 7
  • https://stackoverflow.com/questions/21822526/can-we-add-text-to-a-drawable – jose praveen Apr 04 '20 at 04:07
  • Does this answer your question? [Can we add text to a drawable?](https://stackoverflow.com/questions/21822526/can-we-add-text-to-a-drawable) – Raul Sauco Apr 04 '20 at 07:03
  • Thanks for the link. Very helpfull. Turns out i was not declaring my Bitmap correctly. This solved the problem picIV.buildDrawingCache(); Bitmap bmap = picIV.getDrawingCache(); – Chewee133 Apr 04 '20 at 15:58

1 Answers1

0

Thanks for the link. Very helpful. Turns out i was not declaring my Bitmap correctly.

This solved the problem

            picIV.buildDrawingCache();
            Bitmap bitmap= picIV.getDrawingCache();

Its a deprecated method, but it works.

Chewee133
  • 55
  • 7