3

I wanted to convert string of EditText to Bitmap. have a string like this

String str=edtext.getText().toString(); 

How can i convert that string to bitmap?

MKJParekh
  • 34,073
  • 11
  • 87
  • 98
Ashish Augustine
  • 1,784
  • 4
  • 20
  • 50

3 Answers3

7

I dont know about how to make image of that string but here is code to make Bitmap from and EditText

So you will get Bitmap image of the whole EditText not only String with this,

mEditText.setCursorVisible(false);
mEditText.buildDrawingCache();
Bitmap bmp = Bitmap.createBitmap(mEditText.getDrawingCache());
MKJParekh
  • 34,073
  • 11
  • 87
  • 98
  • He could also create a Canvas and then use drawText method, although is not as easy as your way ;-) – momo Dec 28 '11 at 06:17
  • @Ashish Bhai,You asked the question wrongly so you got weired comments,while asking always be precise to get exact answers. – MKJParekh Dec 28 '11 at 06:37
  • @momo yeh! I suggest if you have code demo for that also put that as an answer so the all can have options to use..cause many like me don't know how to use canvas. – MKJParekh Dec 28 '11 at 06:38
  • @ frank thanks for yuor comment..the code for me was to convert the edit text convert and then merge it as a single image , so as to use it for a photo captioning app. the image here is an image of a 'captioning'. i am putting the code as my answer – Ashish Augustine Dec 28 '11 at 07:08
  • @Ashish I understand,So I have one more idea for you...make one full transparent image from designer,now when user submit caption..then first set that image to edittext and then convert it to bimap and then combine both bitmap caption and image so the white bg of edittext will not come..just and idea not implemented. – MKJParekh Dec 28 '11 at 07:20
  • thnx frnk..i tried to post the answer. but stackovflw says i have to wait for 8 hours ..so iw uill post it tomorrow.. hope it helps you somewhere in ur prgmmg too.. – Ashish Augustine Dec 28 '11 at 07:29
  • @Frankenstein : hi frank, sorry for the delay.. i have added the answer ..hope it helps at some point..happy new year!!.. – Ashish Augustine Dec 30 '11 at 07:42
  • I am missing something, can't create bitmap, nullpointexception for myTextView.getDrawingCache() ? – Abdul Wahab Dec 01 '12 at 10:15
  • this code is really helpful. I am assigning that image generated to a image view but each time when I edit that text in Edittext I am not getting Updated Image. – Jogendra Gouda Mar 31 '14 at 05:26
  • 1
    @JogendraGouda You will need to created new image each time and set that to Imageview again, for that you can move that code to EditText's onTextChange method, take care of recycling the unused bitmap else you will run out of memory. – MKJParekh Mar 31 '14 at 06:23
  • @MKJParekh Thank you for yr comment. can you please show me some part of your code. – Jogendra Gouda Mar 31 '14 at 09:22
  • my code? which code you want to see? creating bitmap is up there, you also have done that in your app. @JogendraGouda – MKJParekh Mar 31 '14 at 11:39
  • @MKJParekh EditText et = (EditText) findViewById(R.id.editText1); et.setCursorVisible(false); et.buildDrawingCache(); Bitmap bmp = Bitmap.createBitmap(et.getDrawingCache()); ImageView img = (ImageView) findViewById(R.id.imageView1); img.setImageBitmap(bmp); This is my code still It is not working – Jogendra Gouda Apr 01 '14 at 05:26
2

I have used the following solution to solve my problem,and this worked for me.

Bitmap bmp = Bitmap.createBitmap(edtext.getDrawingCache());
System.out.println("ashish"+edtext.getText().toString());
Bitmap bm = BitmapFactory.decodeResource(r, R.drawable.balloon_overlay_focused);
Bitmap bmw=combineImages( bm,bmp);
CompositeImageViewText.setImageBitmap(bmw);

//code for combineimages()

public Bitmap combineImages(Bitmap c, Bitmap s) { // can add a 3rd parameter 'String loc' if you want to save the new image - left some code to do that at the bottom 
        Bitmap cs = null; 

        int width, height = 0; 

        if(c.getWidth() > s.getWidth()) { 
          width = c.getWidth(); 
          height = s.getHeight()+30 ; 
        } else { 
          width = s.getWidth(); 
          height = s.getHeight()+30 ; 
        } 

        cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 

        Canvas comboImage = new Canvas(cs); 

        comboImage.drawBitmap(c, 0f, 0f, null); 
        comboImage.drawBitmap(s, 0f, 0f, null); 

        // this is an extra bit I added, just incase you want to save the new image somewhere and then return the location 
        /*String tmpImg = String.valueOf(System.currentTimeMillis()) + ".png"; 

        OutputStream os = null; 
        try { 
          os = new FileOutputStream(loc + tmpImg); 
          cs.compress(CompressFormat.PNG, 100, os); 
        } catch(IOException e) { 
          Log.e("combineImages", "problem combining images", e); 
        }*/ 

        return cs; 
      } 

Hope it helps other !

MKJParekh
  • 34,073
  • 11
  • 87
  • 98
Ashish Augustine
  • 1,784
  • 4
  • 20
  • 50
0

A "bitmap" is the set of pixels that make up an image.

A "string" is a set of characters that make up a word.

The best you can do is read a bitmap, based on the bitmap's file name. That's what Ankit Awasthi illustrated above.

Hopefully that's what you're looking for...

paulsm4
  • 114,292
  • 17
  • 138
  • 190