[英]How to set images from assets folder to list view?(using SimpleAdapter)
I've got folder in assets called images1 with 114 images in it.我有一个名为images1 的资产文件夹,其中有 114 张图像。 I need to set them in listView with 2 textViews and 1 imageView.我需要用 2 个 textViews 和 1 个 imageView 在 listView 中设置它们。 I haven't problems with textViews, but i don't know how to set images from assets to listView.我对 textViews 没有问题,但我不知道如何将图像从资产设置为 listView。 I tried:我试过:
int ids[] = new int[114];
for (int i = 0; i <ids.length; i++) {//<-------- taking ids of all pictures from images1 in assets-folder
try {
String[] images =getAssets().list("images1");
ArrayList<String> listImages = new ArrayList<String>(Arrays.asList(images));
int imgId = getResourceId(this, listImages.get(i),"images1", getPackageName());
ids[i] = imgId;
}
catch(IOException ex) {}
}
ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>(
questionTexts.length);//<--------filling listView's textViews and imageView
Map<String, Object> m;
for (int i = 0; i < questionTexts.length; i++) {
m = new HashMap<String, Object>();
m.put(ATTRIBUTE_QUESTION_TEXT, questionTexts[i]);//<-------- textView
m.put(ATTRIBUTE_ANSWER_TEXT, answerTexts[i]);//<-------- textView
m.put(ATTRIBUTE_NAME_IMAGE, ids[i]);//<-------- imageView
data.add(m);
String[] from = { ATTRIBUTE_QUESTION_TEXT, ATTRIBUTE_ANSWER_TEXT,
ATTRIBUTE_NAME_IMAGE };
int[] to = { R.id.listView_item_title, R.id.listView_item_short_description, R.id.listView_image };
SimpleAdapter sAdapter = new SimpleAdapter(this, data, R.layout.item,
from, to);
lvSimple = (ListView) findViewById(R.id.lvSimple);
lvSimple.setAdapter(sAdapter);
}
public static int getResourceId(Context context,String variableName, String resourceName,
String packageName) throws RuntimeException{//<----- this method helps me to get IDs of images from assets/images1
try{
return context.getResources().getIdentifier(variableName,resourceName,packageName);
}catch (Exception e){
throw new RuntimeException("Error getting resource id");
}
}
But finally i've got white fields instead my pictures.但最后我有白色的田野而不是我的照片。 I know how to solve this problem when your pictures are in R.drawable, but how to do it when they are in assets subfolder?我知道当你的图片在 R.drawable 中时如何解决这个问题,但是当它们在资产子文件夹中时如何解决?
You can use this.你可以用这个。
try
{
// get input stream
InputStream ims = getAssets().open("avatar.jpg");
// load image as Drawable
Drawable d = Drawable.createFromStream(ims, null);
// set image to ImageView
mImage.setImageDrawable(d);
ims .close();
}
catch(IOException ex)
{
return;
}
You can get a bitmap by using below code您可以使用以下代码获取位图
private Bitmap getBitmapFromAssets(String fileName){
AssetManager am = getAssets();
InputStream is = null;
try{
is = am.open(fileName);
}catch(IOException e){
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(is);
return bitmap;
}
You can show bitmap in imageview by using "Glide" Here is sample code您可以使用“Glide”在imageview中显示位图这是示例代码
Glide.with(context)
.load(Uri.parse("file:///android_asset/fileName"))
.into(imageView);
Are you sure that this images should be inside assets/
folder?你确定这个图片应该在assets/
文件夹中吗? Why not in res/drawables/
?为什么不在res/drawables/
?
Of course you can load it from assets ;)当然,您可以从资产加载它;)
To get the list of all files inside asset folder use below code:要获取资产文件夹中所有文件的列表,请使用以下代码:
list = getAssets().list("images1")
To load the image from asset you can use below code:要从资产加载图像,您可以使用以下代码:
fun setImageFromAsset(ImageView view, String filename) {
try {
InputStream is = getAssets().open(filename);
Drawable drawable = Drawable.createFromStream(is, null);
view.setImageDrawable(drawable);
}
catch(IOException ex) {
return;
}
}
Simple Adapter can work only with IDs in drawable-folder or with Uri. Simple Adapter 只能使用 drawable-folder 或 Uri 中的 ID。 But you can use ViewBinder to set image using setImageBitmap method.但是您可以使用 ViewBinder 使用 setImageBitmap 方法设置图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.