Mihaela Miki
Mihaela Miki

Reputation: 75

How to declare an ImageView in a class?

I have an InsertStampActivity witch contains 3 EditText and an ImageView received from phone's gallery. I need to create a class with those 3 editText and the image.

Here is my class:

public class Timbru {
    private int year;
    private String country;
    private float value;    } 

I have also implement construtor, getters and setters for each parameter.

My question is how to implement the imageView in my class for further operations?

Upvotes: 0

Views: 107

Answers (1)

Belen
Belen

Reputation: 26

You could declare your image in your class as a String.

public class Timbru {
   private int year;
   private String country;
   private float value;
   private String image;
}

Then, in your activity, convert the ImageView to a String like this:

Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.yourImage);
ByteArrayOutputStream stream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] image=stream.toByteArray();


String imageToStoreInYourClass = Base64.encodeToString(image, 0);

And then you can also convert from the String to Bitmap back like this:

byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

Upvotes: 1

Related Questions