繁体   English   中英

如何将资产文件夹中的图像设置为列表视图?(使用 SimpleAdapter)

[英]How to set images from assets folder to list view?(using SimpleAdapter)

我有一个名为images1 的资产文件夹,其中有 114 张图像。 我需要用 2 个 textViews 和 1 个 imageView 在 listView 中设置它们。 我对 textViews 没有问题,但我不知道如何将图像从资产设置为 listView。 我试过:

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");
    }
}

但最后我有白色的田野而不是我的照片。 我知道当你的图片在 R.drawable 中时如何解决这个问题,但是当它们在资产子文件夹中时如何解决?

你可以用这个。

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;
}

您可以使用以下代码获取位图

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;
}

您可以使用“Glide”在imageview中显示位图这是示例代码

Glide.with(context)
    .load(Uri.parse("file:///android_asset/fileName"))
    .into(imageView);

你确定这个图片应该在assets/文件夹中吗? 为什么不在res/drawables/

当然,您可以从资产加载它;)

要获取资产文件夹中所有文件的列表,请使用以下代码:

list = getAssets().list("images1")

要从资产加载图像,您可以使用以下代码:

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 只能使用 drawable-folder 或 Uri 中的 ID。 但是您可以使用 ViewBinder 使用 setImageBitmap 方法设置图像。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM