Joseph Joseph February 2016

How to get Image's string value(or an Image reference) from a ImageView

I have assigned a textview and a ImageView as below,

Text = (TextView) findViewById(R.id.TextView01);
Image = (ImageView) findViewById(R.id.ImageView01);

Now their values will change dynamically in my program (Image changed from a online url), and at the end I need to store their values in db for future use. here i get text value as

String TextValue = Text.getText().toString();

but how I store value from ImageView?

String ImageValue = Image.someFunctionThatReturnsImageReferenceInString();

Is there any such functions??
Or how to do this.. Pls help

Answers


AKSiddique February 2016

Unfortunately, there is no getImageResource() or getDrawableId(). Try this one may be it will help full for you.

imageView1 = (ImageView) findViewById(R.id.imageView1);
imageView2 = (ImageView) findViewById(R.id.imageView2);
imageView1.setTag(R.drawable.apple);
imageView2.setTag(R.drawable.banana);

If you like, you can create a simple function to get the drawable id:

private int getDrawableId(ImageView iv) {
    return (Integer) iv.getTag();
}


Hein February 2016

You can get Drawable like this

Drawable drawable = Image.getDrawable();

Or Bitmap like this :

Bitmap bitmap = ((BitmapDrawable)Image.getDrawable()).getBitmap();


Shree Krishna February 2016

I think you are trying to get the imageName of a specific image that was retrieved from URL which may looks like this. Your Image URL

http://example.com/foo/bar/myImage.png

Now you can do

URI uri = new URI("http://example.com/foo/bar/myImage.png
");
String path = uri.getPath();
String imageName = path.substring(path.lastIndexOf('/') + 1);

And you can do whatever you want with imageName


Vasant February 2016

you need to convert image-view into base64 string like below.

1]convert imageview to bitmap.

  imageView.buildDrawingCache();
  Bitmap bmap = imageView.getDrawingCache();

2]bitmap to base64 String.

public String getEncoded64ImageStringFromBitmap(Bitmap bitmap) {
      ByteArrayOutputStream stream = new ByteArrayOutputStream();
      bitmap.compress(CompressFormat.JPEG, 70, stream);
      byte[] byteFormat = stream.toByteArray();
      // get the base 64 string
      String imgString = Base64.encodeToString(byteFormat, Base64.NO_WRAP);
  return imgString;
}

3]base64 to bitmap.

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

set this bitmap to imageview.

Post Status

Asked in February 2016
Viewed 1,087 times
Voted 13
Answered 4 times

Search




Leave an answer