0

How to change or transform an ImageView to a object or a string? So, I can send that imagview as a String to API.

Example, I have 4 id imageview from xml :

@+id/q1
@+id/q2
@+id/q3
@+id/q4

and i want to change all those imageviews to be a string in Java. So, i can send them to API as a JSON like this :

 {
     option_code: 'rp001o001', 
     option_value: 'Likuiditas dan keamanan investasi'
    }

option_code: 'rp001o001' is the paramater that i get from ImageView. How to make it possible?

Thank you.

3 Answers3

0

byte[] is best way to send data from android phone to server , You need to convert Image into byte[] check below method

// convert from bitmap to byte array

public byte[] getBytesFromBitmap(Bitmap bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.JPEG, 70, stream);
    return stream.toByteArray();
}

// get the base 64 string

String imgString = Base64.encodeToString(getBytesFromBitmap(someImg), 
                       Base64.NO_WRAP);

Thank for this guy -https://stackoverflow.com/a/5333971/4741746

Community
  • 1
  • 1
Sushant Gosavi
  • 3,647
  • 3
  • 35
  • 55
0

First get the image as an bitmap from the imageview and convert them into base64 string. Here's how you do it:

BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmapOrg= drawable.getBitmap();
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 100, bao);
byte [] ba = bao.toByteArray();
String ba1=Base64.encodeToString(ba,Base64.DEFAULT);

Here ba1 is your image string.

Karun Shrestha
  • 731
  • 7
  • 16
-1

just convert your image to a base 64 string and store it in your server and do the reverse to show the image in your app later

how to convert an image into base64 string

Community
  • 1
  • 1