1

In my app, I have some small png images in drawable folder which can be displayed in edittext and textviews.

Right now, I'm able to display these images in edittext and textview.

I have the following scenario: At User1, he can select the image and it gets displayed in the edittext. On clicking a send button, the text inside edittext needs to get sent to User2.

I'm using GCM for sending normal text in between users.

So, how can I represent the image in text format. What coding should I use? Should I just have drawable image file name prepended by some special characters?

For example: If image name is e205, then can something like the following be done: "This is normal text, ##e205 previous is image"

Like I used ## in this case, what can be used for identifying that following word is actually an image filename and not normal text?

When User2 gets this message, only the coded part of text should get converted back to image, and get displayed in a textview.

user5155835
  • 4,392
  • 4
  • 53
  • 97

2 Answers2

0

You can use this as explained here: Drawable to byte[]

Drawable d; // the drawable (Captain Obvious, to the rescue!!!)
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitmapdata = stream.toByteArray();

Once done you can make it a String:

new String(bitmapdata);

Then you can send it through a socket, gcm or whatever you want.

Is up to you to make a protocol so the app can diferentiate between coded image and texts.

If both Clients have the drawable in local storage you only need to tell them the name of the drawable so they will be able to load and display it.

You should define a separator that will help to split the string into image / text

Something like this can be your string "IMAGENAME|USERTEXT", just make sure you dont use any character that can be used in the text entered by the user.

Then, on the other side, you can split it with

split("|")

In this case it will return an String array of size 2, IMAGENAME on position 0, USERTEXT on position 1.

Hope this helps.

Community
  • 1
  • 1
Nanoc
  • 2,381
  • 1
  • 20
  • 35
  • Should I just have drawable image file name prepended by some special characters? – user5155835 Sep 25 '15 at 12:16
  • Yea you should just enclose it with some character that will never be used otherwise – Nanoc Sep 25 '15 at 12:24
  • Like? Please can you suggest? Also, I have updated the question – user5155835 Sep 25 '15 at 12:25
  • I just read that both clients have the drawable in local storage, then you just need the drawable name, you can use something like ^%IMAGENAME^%USERTEXT, anything than will not be used commonly, then use String.split("^%") on the receiver side. – Nanoc Sep 25 '15 at 12:27
  • Please update your answer with this and I'll mark your answer – user5155835 Sep 25 '15 at 12:30
0

You can simply convert/build the message json and use this json string to pass the GCM. For example see below the message send by user1 to user should look.

{
  "message": {
    "id": 12345,
    "from_name": "John",
    "to_name": "Steave,
    "body": "\"Hello\"",
    "image_id": "\"1.png\""
  }
}

Now in side of user2. When user2 receive the message [GCM] you get the above json string. convert the json string into the object or simply parse it. extract the content what you want. for example in your case the image_id. Match the image id in you local directory or assets and use it for display for user2.

Let me know if you need more information.

Thank you.

Bhavdip Sagar
  • 1,951
  • 15
  • 27