Search code examples
androidcanvasviewbitmapandroid-custom-view

Write text on image and save the image with text in the SD card without drawing it on the screen


I have been struggling with this problem for quite a long time now. I want to take an image from the resources folder and write some text, dynamically generated as part of a process, and then save the final image with text in the SD Card. I know how to write the files in the SD card. I'm unable to write the text on the image.

I created a RelativeLayout with imageview and textview and saved it to SD card but later realized that I don't have to draw the view. So, couldn't go with this.

Again, I would like to stress the point that my application requires not to draw the bitmap on the current screen.

Could anyone offer any solutions here?

Thanks! :)


Solution

  • Dont use an image view for this.

    Use canvas!

    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.image);
    Bitmap alteredBitmap = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), bm.getConfig());
    Canvas canvas = new Canvas(alteredBitmap);
    Paint paint = new Paint();
    canvas.drawBitmap(bm, 0, 0, paint);
    paint.setColor(Color.BLACK); 
    paint.setTextSize(20); 
    canvas.drawText("Some Text", 10, 25, paint); 
    

    Then just save 'alteredBitmap' to the SD card