0

I want to be able to print text on an image in my android app.

Currently, the code below allows the picture and text to be shared separately from each other. Please refer to the picture to see the result. How can I get the text to print on the image?

Please help me out. I would really appreciate.

holder.shareBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        bitmapDrawable = (BitmapDrawable) holder.imageView.getDrawable();// get the from imageview or use your drawable from drawable folder
        bitmap1 = bitmapDrawable.getBitmap();
        String imgBitmapPath= MediaStore.Images.Media.insertImage(context.getContentResolver(),
                bitmap1,"title",null);
        Uri imgBitmapUri=Uri.parse(imgBitmapPath);
        String shareText=paheliItem.getTitle();
        Intent shareIntent=new Intent(Intent.ACTION_SEND);
        shareIntent.setType("*/*");
        shareIntent.putExtra(Intent.EXTRA_STREAM,imgBitmapUri);
        shareIntent.putExtra(Intent.EXTRA_TEXT, shareText);
        context.startActivity(Intent.createChooser(shareIntent,"Share Wallpaper using"));

    }
});

Image- result on clicking shareBtn

1 Answers1

0

see code below:

private void drawTextOnBitmap(Bitmap bmp, String text, int x, int y, int textColor) {
        Canvas canvas = new Canvas(bmp);

        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(textColor);
        // draw text
        canvas.drawText(text, x, y, paint);
    }


holder.shareBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        bitmapDrawable = (BitmapDrawable) holder.imageView.getDrawable();// get the from imageview or use your drawable from drawable folder
        // draw text on bitmap
        Bitmap bitmap1 = bitmapDrawable.getBitmap();
        drawTextOnBitmap(bitmap1, "YOUR TEXT", 0, 15, Color.RED);


        String imgBitmapPath= MediaStore.Images.Media.insertImage(context.getContentResolver(),
                bitmap1,"title",null);
        Uri imgBitmapUri=Uri.parse(imgBitmapPath);
        String shareText=paheliItem.getTitle();
        Intent shareIntent=new Intent(Intent.ACTION_SEND);
        shareIntent.setType("*/*");
        shareIntent.putExtra(Intent.EXTRA_STREAM,imgBitmapUri);
        shareIntent.putExtra(Intent.EXTRA_TEXT, shareText);
        context.startActivity(Intent.createChooser(shareIntent,"Share Wallpaper using"));

    }
});
Phil
  • 201
  • 1
  • 7
  • Android Studio was displaying an error- so I had to change the return type to Bitmap and return bmp (private Bitmap drawTextOnBitmap). Now I am getting this error: java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor. Please help. – Abdul Rehman Jan 26 '22 at 14:40
  • I added the 2 lines of code provided by Ujju (https://stackoverflow.com/a/48746530/8945206) into the **drawTextOnBitmap()** method and now the problem is sorted! To summarise, I just had to define a variable **Bitmap workingBitmap = bmp;** in the method, with those 2 lines of code and end the method with **return workingBitmap**. Thank you so much for your help! – Abdul Rehman Jan 26 '22 at 20:00
  • Yes, i see the error too. I write code in sublime text editor, so do not test in android studio, sorry for that. – Phil Jan 27 '22 at 02:29
  • Not a problem. **You've been super helpful! Thank you once again.** – Abdul Rehman Jan 27 '22 at 23:53