2

I'm trying to get the text input from the user and draw it on the image using Canvas but the image is saved without what was supposed to be drawn. Right now, I'm just trying to get the text on the Image before I worry about the font, colour, styles, etc.

This is my code:

 public void createBitmapAndSave(ImageView img){
        BitmapDrawable bitmapDrawable = ((BitmapDrawable) img.getDrawable());
        Bitmap bitmap = bitmapDrawable.getBitmap();
        Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);

        Canvas canvas = new Canvas(mutableBitmap);
        Paint paint = new Paint();
        paint.setColor(Color.BLUE);
        paint.setTextSize(200);
        paint.setStyle(Paint.Style.FILL);
        paint.setShadowLayer(10f, 10f, 10f, Color.BLACK);

        String topText = topTextView.getText().toString();
        String bottomText = bottomTextView.getText().toString();

        canvas.drawText(topText, 0, 0, paint);
        canvas.drawText(bottomText, 50, 50, paint);

        File file;
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);

        String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath();
        file = new File(path + "/SimpliMeme/" + timeStamp + "-" + counter + ".jpg");
        file.getParentFile().mkdir();

        try{
            OutputStream stream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG,100,stream);
            stream.flush();
            stream.close();
            Toast.makeText(getContext(), "Meme Saved", Toast.LENGTH_SHORT).show();
        }
        catch (IOException e){ e.printStackTrace();}

        Uri contentUri = Uri.fromFile(file);
        mediaScanIntent.setData(contentUri);
        Objects.requireNonNull(getContext()).sendBroadcast(mediaScanIntent);
        counter++;
    }

At the moment, I only have the 2 .drawText() implementations based on the examples that I've seen in other SO posts. My assumption is that the text isn't visible and no changes are made to the image because I haven't provided the paint object with any attributes.

  • You didn't setup your `Paint` object in any way. No color, no font, no text size... Here's something to get you going: https://developer.android.com/training/custom-views/custom-drawing – Eugen Pechanec Feb 26 '19 at 19:18
  • @EugenPechanec That's what I was wondering. Can't I draw text with the default font and color? Is the paint object a must to draw anything? – Andros Adrianopolos Feb 26 '19 at 19:42
  • @EugenPechanec Check the new code now. I've added attributes to the paint object and yet nothing is drawn on the bitmap. – Andros Adrianopolos Feb 26 '19 at 19:45
  • 2
    `Is the paint object a must to draw anything?` Yeah, that's the whole point of the class. Just read the [docs](https://developer.android.com/reference/android/graphics/Paint.html): `The Paint class holds the style and color information about how to draw geometries, text and bitmaps.` As for `nothing is drawn on the bitmap` You changed `mutableBitmap` and saved `bitmap`, this is why you see no changes. – Eugen Pechanec Feb 26 '19 at 19:49
  • @EugenPechanec Thank you very much. It worked. – Andros Adrianopolos Feb 26 '19 at 19:59
  • I think @EugenPechanec should create an answer then AndrosAdrianopolos accept it.. – ישו אוהב אותך Feb 27 '19 at 04:37
  • I'm almost tempted to have this question closed as a typo, because hey, it wasn't about drawing text or Paint, it was *just* about the wrong variable. This can happen to you while solving any kind of problem, it's not specific to `Android canvas doesn't draw the text on my bitmap`. – Eugen Pechanec Feb 27 '19 at 07:14

1 Answers1

1

The main issue why you see no changes is that you make changes to mutableBitmap but save the original bitmap to disk.

This can be avoided by joining the first two (or even three) statements together:

final Bitmap bitmap = bitmapDrawable.getBitmap()
        .copy(Bitmap.Config.ARGB_8888, true);

You didn't need the orginal bitmap anywhere else, this effectively prevents you from making the mistake. Don't do what you don't need to do.

Some tips:

Eugen Pechanec
  • 34,848
  • 7
  • 94
  • 115