I have an ImageView
and I want get the path of image and add in a String
. I want do this because I need send this to my webservice like a default image
How can I do it ?
ImageView
<ImageView
android:id="@+id/ivPerfil"
android:padding="5dp"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="@drawable/icon_usuario"
android:scaleType="fitXY"
/>
Getting the path
ivPerfil = (ImageView)findViewById(R.id.ivPerfil);
Drawable img = getResources().getDrawable(R.drawable.icon_usuario);
String pathImage = img.toString();
You can't directly get the path to the image referenced by the
ImageView
, but there is a bit of a way around it.Since you clearly know the path to the image when setting it (since you wouldn't be able to set it without knowing it), you have the option to set the
View's
tag (which can technically be anyObject
).In this case, it's just a
String
you're going to want to store.So let's say you have:
Now, when setting the image Uri on the
ImageView
, add this after to set its tag:Then, when you're actually going to send it to the server, just call the
getTag()
method on yourImageView
and you'll get the correct file location.e.g.
If the image is from your assets, though, you're going to have to construct the appropriate path from the resources (see miselking's answer on this page for how to do that).
You can try this:
"your.package.name" is the name of your package (i.e: com.example.mypackage)
You can use this path in android like this: