1

I need to convert string in the edittext to bitmap, but i am not getting the string but instead i am getting this (see the image)enter image description here

my code is as follows

 Canvas c=new Canvas();

     MainActivity.editText.setCursorVisible(false);
     MainActivity.editText.buildDrawingCache();
     Bitmap bmp = Bitmap.createBitmap(MainActivity.editText.getDrawingCache());

     System.out.println("string is "+MainActivity.editText.getText().toString());


File f =new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Photo Text");
     if(!f.exists())
     {
         f.mkdirs();
     }
     f = new File(f.getAbsolutePath(),
             String.valueOf(System.currentTimeMillis()) +"phototext.jpg");
     if(!f.exists())
     {
         try {
            f.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
     }


     try {
         bmp.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(f));
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


     c.drawBitmap(bmp,0,0, mPaint);

Please suggest me. I need the string from the edittext as a bitmap.

Sandeep R
  • 2,284
  • 3
  • 25
  • 51

1 Answers1

1

Why not get the text of the ExitText and then draw it on Canvas with drawText()?

String text = editText.getText().toString();
canvas.drawText(text, 0, 0, paint);

and set the canvas height and width depending of text height and length.

Andy Res
  • 15,963
  • 5
  • 60
  • 96
  • Thanks this worked, but I think creating a bitmap from the string is better because I need to move the text ontouch to position it on another bitmap. can you suggest some more possibilities as per my requirements. – Sandeep R Oct 04 '13 at 07:25