【问题标题】:How can I convert an int expression (from drawabale) into String expression in Android Studio (JAVA)如何在Android Studio(JAVA)中将int表达式(来自drawabale)转换为String表达式
【发布时间】:2020-10-26 14:31:15
【问题描述】:

我想转换我用这个获得的 int 表达式:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_full_screen_image);
    imageViewFullScreen = findViewById(R.id.imageView_fullscreen);
    textViewFullScreen = findViewById(R.id.textView_fullscreen);
    try
    {
        Bundle bundle = getIntent().getExtras();
        imageId = bundle.getInt("imageId");
    }

转换成字符串表达式,但是当我尝试这样做时,它不起作用。

我尝试将它放在 TextView 中,使用 int 表达式我有正确的东西,但使用 String 我只有数字而不是文本。

我想这样做是为了剪掉我感兴趣的字符串的一部分。

有什么解决方案可以让我想要的东西不通过 String 传递吗?或正确转换我的 int 表达式?

我已经尝试过这样做:

imageViewFullScreen.setImageResource(imageId);
        textViewFullScreen.setText("");
        textViewFullScreen.append("Ref : " + imageId);

我想要:res/drawable/cata_01jpg,但我有:2131099745

谢谢大家!

【问题讨论】:

标签: java android string parsing int


【解决方案1】:

您可以使用此函数返回带有资源名称的路径:

    String path = getPath(R.drawable.icon);
    public String getPath(int id) {
    return Uri.parse("res/drawable/" + getResources().getResourceEntryName(id)).toString();
}

【讨论】:

    【解决方案2】:

    您拥有的数字是资源标识符。您可以使用 Resources 类来查找有关它所引用的资源的信息。这个类有几个方法,我不知道哪个对你最有用,但getResourceEntryName 听起来很接近。

    String name = getResources().getResourceEntryName(imageId);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-19
      • 2017-12-15
      相关资源
      最近更新 更多