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
我有一个ImageView,我想获取图像的路径并添加一个字符串。我想这样做是因为我需要将它发送到我的webservice,就像默认图像一样
How can I do it ?
我该怎么做 ?
ImageView
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();
2 个解决方案
#1
3
You can try this:
你可以试试这个:
Uri path = Uri.parse("android.resource://your.package.name/" + R.drawable.icon_usuario);
String imgPath = path.toString();
"your.package.name" is the name of your package (i.e: com.example.mypackage)
“your.package.name”是您的包的名称(即:com.example.mypackage)
You can use this path in android like this:
您可以在android中使用此路径,如下所示:
ivPerfil = (ImageView)findViewById(R.id.ivPerfil);
ivPerfil.setImageURI(path);
#2
5
You can't directly get the path to the image referenced by the ImageView
, but there is a bit of a way around it.
您无法直接获取ImageView引用的图像的路径,但有一些方法可以解决它。
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 any Object
).
由于您在设置图像时清楚地知道图像的路径(因为您无法在不知情的情况下进行设置),您可以选择设置视图标记(技术上可以是任何对象)。
In this case, it's just a String
you're going to want to store.
在这种情况下,它只是一个你想要存储的String。
So let's say you have:
所以,假设你有:
Uri imageFilePath = Uri.parse("some/path/to/file.png");
Now, when setting the image Uri on the ImageView
, add this after to set its tag:
现在,在ImageView上设置图像Uri时,请在之后添加以设置其标记:
imageView.setTag(imageFilePath.toString());
Then, when you're actually going to send it to the server, just call the getTag()
method on your ImageView
and you'll get the correct file location.
然后,当您实际要将其发送到服务器时,只需在ImageView上调用getTag()方法,您就可以获得正确的文件位置。
e.g.
例如
String path = imageView.getTag().toString();
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).
但是,如果图像来自您的资产,则您将不得不从资源构建适当的路径(有关如何执行此操作,请参阅此页面上的miselking的答案)。
#1
3
You can try this:
你可以试试这个:
Uri path = Uri.parse("android.resource://your.package.name/" + R.drawable.icon_usuario);
String imgPath = path.toString();
"your.package.name" is the name of your package (i.e: com.example.mypackage)
“your.package.name”是您的包的名称(即:com.example.mypackage)
You can use this path in android like this:
您可以在android中使用此路径,如下所示:
ivPerfil = (ImageView)findViewById(R.id.ivPerfil);
ivPerfil.setImageURI(path);
#2
5
You can't directly get the path to the image referenced by the ImageView
, but there is a bit of a way around it.
您无法直接获取ImageView引用的图像的路径,但有一些方法可以解决它。
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 any Object
).
由于您在设置图像时清楚地知道图像的路径(因为您无法在不知情的情况下进行设置),您可以选择设置视图标记(技术上可以是任何对象)。
In this case, it's just a String
you're going to want to store.
在这种情况下,它只是一个你想要存储的String。
So let's say you have:
所以,假设你有:
Uri imageFilePath = Uri.parse("some/path/to/file.png");
Now, when setting the image Uri on the ImageView
, add this after to set its tag:
现在,在ImageView上设置图像Uri时,请在之后添加以设置其标记:
imageView.setTag(imageFilePath.toString());
Then, when you're actually going to send it to the server, just call the getTag()
method on your ImageView
and you'll get the correct file location.
然后,当您实际要将其发送到服务器时,只需在ImageView上调用getTag()方法,您就可以获得正确的文件位置。
e.g.
例如
String path = imageView.getTag().toString();
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).
但是,如果图像来自您的资产,则您将不得不从资源构建适当的路径(有关如何执行此操作,请参阅此页面上的miselking的答案)。