0

私のアプリでは、xmlファイルを使用してデータを保存しており、画像はドローアブルフォルダーに保存されています。これらを「リンク」したいと思います。つまり、xmlファイル内の各オブジェクトは、ドローアブルフォルダー内の特定の画像を参照します。どうすればこれを達成できますか?ありがとう!

<contacts>
    <contact>
        <name>Joe</name>
        <picture>R.drawable.joe_pic</picture>
    </contact>
    <contact>
        <name>Dean</name>
        <picture>R.drawable.dean_pic</picture>
    </contact>
</contacts>
4

1 に答える 1

2

「joe_pic」のような名前を保存するだけで試すことができ、この方法で取得できます

private void showImage() {
    String uri = "drawable/icon";

    // int imageResource = R.drawable.icon;
    int imageResource = getResources().getIdentifier(uri, null, getPackageName());

    ImageView imageView = (ImageView) findViewById(R.id.myImageView);
    Drawable image = getResources().getDrawable(imageResource);
    imageView.setImageDrawable(image);
}

ただし、すでに「R.drawable.joe_pic」を追加している場合は、String.splitを使用して3番目の文字列部分を取得できます。

于 2012-06-26T10:18:41.587 に答える