Я создаю приложение для Android на Kotlin и ищу способ отобразить рисуемый объект в моем компонуемом объекте на основе его пути к ресурсу. Для своих целей я не могу использовать целые числа, карты и т. д. Некоторое время назад я нашел этот код для Java, который позволил мне, используя соответствующую логику, динамически создавать и использовать для этого строки. Это было именно то, что я ищу, потому что для моих нужд мне нужно обеспечить, чтобы путь для отображения объекта прописывался динамически. К сожалению, код, о котором я говорю, написан на Java и в самых последних версиях Android выдает ошибки. Я не нашел ничего для Kotlin и не знаю, как правильно перевести код, чтобы вставить его в свою компоновку. Есть предложения? Ниже приведен код Java, о котором я говорю. Извините за мой плохой английский.
String region = nextImage.substring(0, nextImage.indexOf('-'));
//Next we get assetmanager, then use it in the "try-with-resources"
//statement to open an inputStream to read bytes from the flag images file.
//useAssetManager to load next image from assets folder.
//We use that stream as an argument to class Drawables static method createFromStream
//which creates a Drawable object. Then we set it as flagImageViews item to display
//by calling its setImageDrawable method.
AssetManager assets = getActivity().getAssets();
//get an InputStream to the asset representing the next flag
//and try to use the InputStream
try (InputStream stream = assets.open(region + "/" + nextImage + ".png")) {
//load the asset as a Drawable and display on the flagImageView
Drawable flag = Drawable.createFromStream(stream, nextImage);
flagImageView.setImageDrawable(flag);
//animage the flag onto the screen
//Next we call the animate method with false to animate the next flag
//and answer buttons onto the screen.
animate(false);
} catch (IOException exception) {
Log.e(TAG, "Error loading" + nextImage, exception);
}
Подробнее здесь: https://stackoverflow.com/questions/798 ... e-resource